↓Skip to main content
  1. LeetCode/

LeetCode 3548: Equal Sum Grid Partition II

·3 mins· ·
LeetCode Hard Prefix-Sum Hash-Table Matrix Enumeration
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-07-27

  • Submissions: Accepted ×2

Approach
#

Handle only horizontal cuts; for vertical cuts, transpose the grid and run it again. Cutting below row k, accumulate the top sum from row sums (Prefix Sum Template 2), with diff = top - bottom:

  • diff == 0: done.
  • diff > 0: drop a cell with value exactly diff from the top; for diff < 0, drop -diff from the bottom.

The section must stay connected after dropping the cell:

Section shapeWhich cells can be dropped
A single row (or single column)Only the two ends; a middle cell splits it in two
At least 2 × 2Any cell

So we need “does this value appear in the top / bottom part”: record each value’s first and last row. The top is rows 0..k, so its first row must be <= k; the bottom is rows k+1..m-1, so its last row must be >= k + 1.

My Solution
#

class Solution:
    def canPartitionGrid(self, grid: List[List[int]]) -> bool:
        def check(g):                              # horizontal cuts only
            m, n = len(g), len(g[0])
            total = sum(map(sum, g))
            first_row, last_row = {}, {}
            for r in range(m):
                for v in g[r]:
                    first_row.setdefault(v, r)
                    last_row[v] = r
            top = 0
            for k in range(m - 1):                 # cut between rows k and k+1
                top += sum(g[k])
                diff = top - (total - top)
                if diff == 0:
                    return True
                if diff > 0:                       # drop a cell worth diff from the top
                    if k == 0:                     # single row: ends only
                        ok = diff in (g[0][0], g[0][-1])
                    elif n == 1:                   # single column: ends only
                        ok = diff in (g[0][0], g[k][0])
                    else:
                        ok = first_row.get(diff, m) <= k
                else:                              # drop a cell worth -diff from the bottom
                    if k == m - 2:
                        ok = -diff in (g[-1][0], g[-1][-1])
                    elif n == 1:
                        ok = -diff in (g[k + 1][0], g[-1][0])
                    else:
                        ok = last_row.get(-diff, -1) >= k + 1
                if ok:
                    return True
            return False

        return check(grid) or check([list(col) for col in zip(*grid)])

Complexity
#

Time O(m · n), space O(m · n) (the transpose and dictionaries).

Mistakes to Avoid
#

  • In a single-row or single-column section, dropping a middle cell disconnects it; only the ends work.
  • The top uses each value’s first row and the bottom its last row; they cannot share one position.
  • Do not forget vertical cuts: transpose and reuse the same function.

Interview Explanation
#

I handle horizontal cuts and transpose for vertical ones. Accumulating row sums gives the difference between the sides, which must equal a cell on the larger side. If that side is a single row or column, only an end cell can be dropped without disconnecting it; otherwise any cell works, so I record each value’s first and last row to tell whether it lies in that part. Time O(mn).

Related

LeetCode 2122: Recover the Original Array
·3 mins
LeetCode Hard Two-Pointers Sorting Hash-Table Enumeration Array
LeetCode 2122 note: the smallest number is always in lower, so enumerating its partner fixes k; after sorting, two pointers pair each smallest unmatched number with the number 2k above it
LeetCode 980: Unique Paths III
·2 mins
LeetCode Hard Backtracking Matrix Dfs
LeetCode 980 note: grid backtracking that marks visited cells in place and checks at the end that every empty cell was covered
LeetCode 3989: Maximum Consistent Columns in a Grid
·2 mins
LeetCode Hard Dynamic-Programming Matrix Array
LeetCode 3989 note: the kept columns form a subsequence constrained only between neighbors; an LIS-style DP with dp[j] = the longest valid sequence ending at column j
LeetCode 3651: Minimum Cost Path with Teleportations
·2 mins
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Array
LeetCode 3651 note: compute layer by layer over teleports; arriving at a cell by teleport costs the previous layer’s minimum over cells with value >= it, found with a running minimum after sorting, followed by the usual right / down grid DP
LeetCode 1878: Get Biggest Three Rhombus Sums in a Grid
·3 mins
LeetCode Medium Matrix Enumeration Heap
LeetCode 1878 note: enumerate every top vertex and size, walk the four edges, and keep the three largest distinct sums
LeetCode 3910: Count Connected Subgraphs with Even Node Sum
·3 mins
LeetCode Hard Bit-Manipulation Enumeration Graph Union-Find
LeetCode 3910 note: n is tiny, so enumerate subsets with a bitmask, check the even sum first, then check that the induced subgraph is connected