< Summary

Information
Class: LeetCode.Algorithms.EqualSumGridPartition1.EqualSumGridPartition1PrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EqualSumGridPartition1\EqualSumGridPartition1PrefixSum.cs
Line coverage
95%
Covered lines: 42
Uncovered lines: 2
Coverable lines: 44
Total lines: 84
Line coverage: 95.4%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanPartitionGrid(...)93.75%161695.45%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EqualSumGridPartition1\EqualSumGridPartition1PrefixSum.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.EqualSumGridPartition1;
 13
 14/// <inheritdoc />
 15public sealed class EqualSumGridPartition1PrefixSum : IEqualSumGridPartition1
 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 bool CanPartitionGrid(int[][] grid)
 224    {
 225        var m = grid.Length;
 226        var n = grid[0].Length;
 27
 228        long totalSum = 0;
 29
 1230        for (var i = 0; i < m; i++)
 431        {
 432            var row = grid[i];
 33
 2434            for (var j = 0; j < n; j++)
 835            {
 836                var value = row[j];
 37
 838                totalSum += value;
 839            }
 440        }
 41
 242        long topSum = 0;
 243        var bottomSum = totalSum;
 44
 645        for (var i = 0; i < m - 1; i++)
 246        {
 247            var row = grid[i];
 48
 1249            for (var j = 0; j < n; j++)
 450            {
 451                var value = row[j];
 52
 453                topSum += value;
 454                bottomSum -= value;
 455            }
 56
 257            if (topSum == bottomSum)
 158            {
 159                return true;
 60            }
 161        }
 62
 163        long leftSum = 0;
 164        var rightSum = totalSum;
 65
 466        for (var j = 0; j < n - 1; j++)
 167        {
 668            for (var i = 0; i < m; i++)
 269            {
 270                var value = grid[i][j];
 71
 272                leftSum += value;
 273                rightSum -= value;
 274            }
 75
 176            if (leftSum == rightSum)
 077            {
 078                return true;
 79            }
 180        }
 81
 182        return false;
 283    }
 84}