< Summary

Information
Class: LeetCode.Algorithms.FloodFill.FloodFillIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FloodFill\FloodFillIterative.cs
Line coverage
100%
Covered lines: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 52
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FloodFill(...)100%22100%
ReplacePixels(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FloodFill\FloodFillIterative.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.FloodFill;
 13
 14/// <inheritdoc />
 15public class FloodFillIterative : IFloodFill
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="image"></param>
 22    /// <param name="sr"></param>
 23    /// <param name="sc"></param>
 24    /// <param name="newColor"></param>
 25    /// <returns></returns>
 26    public int[][] FloodFill(int[][] image, int sr, int sc, int newColor)
 327    {
 328        if (image[sr][sc] == newColor)
 129        {
 130            return image;
 31        }
 32
 233        ReplacePixels(image, sr, sc, image[sr][sc], newColor);
 34
 235        return image;
 336    }
 37
 38    private static void ReplacePixels(IReadOnlyList<int[]> image, int sr, int sc, int oldColor, int newColor)
 5039    {
 5040        if (sr < 0 || sr >= image.Count || sc < 0 || sc >= image[sr].Length || image[sr][sc] != oldColor)
 3841        {
 3842            return;
 43        }
 44
 1245        image[sr][sc] = newColor;
 46
 1247        ReplacePixels(image, sr - 1, sc, oldColor, newColor);
 1248        ReplacePixels(image, sr + 1, sc, oldColor, newColor);
 1249        ReplacePixels(image, sr, sc - 1, oldColor, newColor);
 1250        ReplacePixels(image, sr, sc + 1, oldColor, newColor);
 5051    }
 52}