< Summary

Information
Class: LeetCode.Algorithms.ScoreAfterFlippingMatrix.ScoreAfterFlippingMatrixIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ScoreAfterFlippingMatrix\ScoreAfterFlippingMatrixIterative.cs
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MatrixScore(...)100%2222100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ScoreAfterFlippingMatrix\ScoreAfterFlippingMatrixIterative.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.ScoreAfterFlippingMatrix;
 13
 14/// <inheritdoc />
 15public class ScoreAfterFlippingMatrixIterative : IScoreAfterFlippingMatrix
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int MatrixScore(int[][] grid)
 624    {
 7625        foreach (var row in grid)
 2926        {
 2927            if (row[0] != 0)
 1328            {
 1329                continue;
 30            }
 31
 8832            for (var j = 0; j < row.Length; j++)
 2833            {
 2834                if (row[j] == 0)
 2035                {
 2036                    row[j] = 1;
 2037                }
 38                else
 839                {
 840                    row[j] = 0;
 841                }
 2842            }
 1643        }
 44
 5445        for (var j = 1; j < grid[0].Length; j++)
 2146        {
 5747            var countOfOnes = grid.Count(row => row[j] == 1);
 48
 2149            if (countOfOnes >= grid.Length / 2.0)
 1150            {
 1151                continue;
 52            }
 53
 6254            foreach (var row in grid)
 1655            {
 1656                if (row[j] == 0)
 1557                {
 1558                    row[j] = 1;
 1559                }
 60                else
 161                {
 162                    row[j] = 0;
 163                }
 1664            }
 1065        }
 66
 667        var score = 0;
 68
 7669        foreach (var row in grid)
 2970        {
 18871            for (var k = 0; k < row.Length; k++)
 6572            {
 6573                if (row[k] == 1)
 5874                {
 5875                    score += (int)Math.Pow(2, row.Length - 1 - k);
 5876                }
 6577            }
 2978        }
 79
 680        return score;
 681    }
 82}

Methods/Properties

MatrixScore(System.Int32[][])