Skip to content

feat: Add solution for 3548. Equal Sum Grid Partition I #4407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 0 additions & 143 deletions solution/3500-3599/3548.Equal Sum Grid Partition II/README.md

This file was deleted.

28 changes: 28 additions & 0 deletions solution/3500-3599/3548.Equal Sum Grid Partition II/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List

class Solution:
def canPartitionGrid(self, grid: List[List[int]]) -> bool:
m, n = len(grid), len(grid[0])

total_sum = sum(sum(row) for row in grid)

# Try horizontal cuts
row_prefix_sum = 0
for i in range(m - 1): # cut between row i and i+1
row_prefix_sum += sum(grid[i])
if row_prefix_sum * 2 == total_sum:
return True

# Try vertical cuts
col_sums = [0] * n
for i in range(m):
for j in range(n):
col_sums[j] += grid[i][j]

col_prefix_sum = 0
for j in range(n - 1): # cut between column j and j+1
col_prefix_sum += col_sums[j]
if col_prefix_sum * 2 == total_sum:
return True

return False
Loading