< Summary

Information
Class: LeetCode.Algorithms.EqualSumGridPartition1.EqualSumGridPartition1PrecomputedPrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EqualSumGridPartition1\EqualSumGridPartition1PrecomputedPrefixSum.cs
Line coverage
89%
Covered lines: 35
Uncovered lines: 4
Coverable lines: 39
Total lines: 80
Line coverage: 89.7%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanPartitionGrid(...)85.71%141489.74%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\EqualSumGridPartition1\EqualSumGridPartition1PrecomputedPrefixSum.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 EqualSumGridPartition1PrecomputedPrefixSum : IEqualSumGridPartition1
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(m + n)
 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        Span<long> rowSums = stackalloc long[m];
 229        Span<long> columnSums = stackalloc long[n];
 30
 231        long totalSum = 0;
 32
 1233        for (var i = 0; i < m; i++)
 434        {
 435            var row = grid[i];
 36
 2437            for (var j = 0; j < n; j++)
 838            {
 839                var value = row[j];
 40
 841                rowSums[i] += value;
 842                columnSums[j] += value;
 843                totalSum += value;
 844            }
 445        }
 46
 247        if (totalSum % 2 != 0)
 048        {
 049            return false;
 50        }
 51
 252        var targetSum = totalSum / 2;
 53
 254        long rowSum = 0;
 55
 656        for (var i = 0; i < m - 1; i++)
 257        {
 258            rowSum += rowSums[i];
 59
 260            if (rowSum == targetSum)
 161            {
 162                return true;
 63            }
 164        }
 65
 166        long columnSum = 0;
 67
 468        for (var j = 0; j < n - 1; j++)
 169        {
 170            columnSum += columnSums[j];
 71
 172            if (columnSum == targetSum)
 073            {
 074                return true;
 75            }
 176        }
 77
 178        return false;
 279    }
 80}