Equal Sum Grid Partition I

medium prefix sum matrix enumeration

Problem

You are given an m × n matrix grid of positive integers. Decide if it is possible to make either one horizontal or one vertical cut so that both resulting sections are non-empty and have equal sums. Return true if such a partition exists, otherwise false.

Inputgrid = [[1,4],[2,3]]
Outputtrue
A horizontal cut between row 0 and row 1 gives two sections, each summing to 5, so the answer is true.

def canPartitionGrid(grid):
    m, n = len(grid), len(grid[0])
    total = sum(v for row in grid for v in row)
    # horizontal cut: grow the top section row by row
    top = 0
    for r in range(m - 1):
        top += sum(grid[r])
        if 2 * top == total:        # top == bottom
            return True
    # vertical cut: grow the left section column by column
    left = 0
    for c in range(n - 1):
        for r in range(m):
            left += grid[r][c]
        if 2 * left == total:       # left == right
            return True
    return False
function canPartitionGrid(grid) {
  const m = grid.length, n = grid[0].length;
  let total = 0;
  for (const row of grid) for (const v of row) total += v;
  // horizontal cut: grow the top section row by row
  let top = 0;
  for (let r = 0; r < m - 1; r++) {
    for (let c = 0; c < n; c++) top += grid[r][c];
    if (2 * top === total) return true;   // top === bottom
  }
  // vertical cut: grow the left section column by column
  let left = 0;
  for (let c = 0; c < n - 1; c++) {
    for (let r = 0; r < m; r++) left += grid[r][c];
    if (2 * left === total) return true;  // left === right
  }
  return false;
}
boolean canPartitionGrid(int[][] grid) {
    int m = grid.length, n = grid[0].length;
    long total = 0;
    for (int[] row : grid) for (int v : row) total += v;
    // horizontal cut: grow the top section row by row
    long top = 0;
    for (int r = 0; r < m - 1; r++) {
        for (int c = 0; c < n; c++) top += grid[r][c];
        if (2 * top == total) return true;   // top == bottom
    }
    // vertical cut: grow the left section column by column
    long left = 0;
    for (int c = 0; c < n - 1; c++) {
        for (int r = 0; r < m; r++) left += grid[r][c];
        if (2 * left == total) return true;  // left == right
    }
    return false;
}
bool canPartitionGrid(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    long long total = 0;
    for (auto& row : grid) for (int v : row) total += v;
    // horizontal cut: grow the top section row by row
    long long top = 0;
    for (int r = 0; r < m - 1; r++) {
        for (int c = 0; c < n; c++) top += grid[r][c];
        if (2 * top == total) return true;   // top == bottom
    }
    // vertical cut: grow the left section column by column
    long long left = 0;
    for (int c = 0; c < n - 1; c++) {
        for (int r = 0; r < m; r++) left += grid[r][c];
        if (2 * left == total) return true;  // left == right
    }
    return false;
}
Time: O(m · n) Space: O(1)