Equal Sum Grid Partition II Problem
Description
LeetCode Problem 3548.
You are given an m x n matrix grid of positive integers. Your task is to determine if it is possible to make either one horizontal or one vertical cut on the grid such that:
- Each of the two resulting sections formed by the cut is non-empty.
- The sum of elements in both sections is equal, or can be made equal by discounting at most one single cell in total (from either section).
- If a cell is discounted, the rest of the section must remain connected.
Return true if such a partition exists; otherwise, return false.
Note: A section is connected if every cell in it can be reached from any other cell by moving up, down, left, or right through other cells in the section.
Example 1:
Input: grid = [[1,4],[2,3]]
Output: true
Explanation:

- A horizontal cut after the first row gives sums 1 + 4 = 5 and 2 + 3 = 5, which are equal. Thus, the answer is true.
Example 2:
Input: grid = [[1,2],[3,4]]
Output: true
Explanation:

- A vertical cut after the first column gives sums 1 + 3 = 4 and 2 + 4 = 6.
- By discounting 2 from the right section (6 - 2 = 4), both sections have equal sums and remain connected. Thus, the answer is true.
Example 3:
Input: grid = [[1,2,4],[2,3,5]]
Output: false
Explanation:

- A horizontal cut after the first row gives 1 + 2 + 4 = 7 and 2 + 3 + 5 = 10.
- By discounting 3 from the bottom section (10 - 3 = 7), both sections have equal sums, but they do not remain connected as it splits the bottom section into two parts ([2] and [5]). Thus, the answer is false.
Example 4:
Input: grid = [[4,1,8],[3,2,6]]
Output: false
Explanation:
No valid cut exists, so the answer is false.
Constraints:
1 <= m == grid.length <= 10^51 <= n == grid[i].length <= 10^52 <= m * n <= 10^51 <= grid[i][j] <= 10^5
Solution
Approach: Enumerate Cuts, Remove One Cell
Handle horizontal cuts first; for vertical cuts transpose the grid and run the same code.
Sweep the cut line from top to bottom while keeping, for the upper and lower part, its sum (s1, s2) and a hash map counting each value. At every cut:
- If
s1 == s2, the answer istrue. - Otherwise let
diffbe the difference. The larger part must lose one cell with value exactlydiff, so lookdiffup in that part's map. - A cell may only be removed if the part stays connected. That holds when the part has at least two rows and at least two columns (some cell can always be taken from a corner), or when the part is a single row (or a single column) and the cell is at one of its two ends. A part with one column and several rows is handled by the same end-of-line rule, since the cell must be its first or last one.
If no cut works in either orientation, return false.
Complexity: O(m * n) time and O(m * n) extra space for the hash maps.
from collections import defaultdict
class Solution:
def canPartitionGrid(self, grid: List[List[int]]) -> bool:
def check(g: List[List[int]]) -> bool:
m, n = len(g), len(g[0])
s1 = s2 = 0
cnt1 = defaultdict(int)
cnt2 = defaultdict(int)
for i, row in enumerate(g):
for j, x in enumerate(row):
s2 += x
cnt2[x] += 1
for i, row in enumerate(g[: m - 1]):
for x in row:
s1 += x
s2 -= x
cnt1[x] += 1
cnt2[x] -= 1
if s1 == s2:
return True
if s1 < s2:
diff = s2 - s1
if cnt2[diff]:
if (
(m - i - 1 > 1 and n > 1)
or (
i == m - 2
and (g[i + 1][0] == diff or g[i + 1][-1] == diff)
)
or (n == 1 and (g[i + 1][0] == diff or g[-1][0] == diff))
):
return True
else:
diff = s1 - s2
if cnt1[diff]:
if (
(i + 1 > 1 and n > 1)
or (i == 0 and (g[0][0] == diff or g[0][-1] == diff))
or (n == 1 and (g[0][0] == diff or g[i][0] == diff))
):
return True
return False
return check(grid) or check(list(zip(*grid)))