Skip to content

Commit e6f8924

Browse files
authored
Create solution.py
adding solution forleetcode solution to cut metrics
1 parent b6b8817 commit e6f8924

File tree

1 file changed

+28
-0
lines changed
  • solution/3500-3599/3548.Equal Sum Grid Partition II

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
class Solution:
4+
def canPartitionGrid(self, grid: List[List[int]]) -> bool:
5+
m, n = len(grid), len(grid[0])
6+
7+
total_sum = sum(sum(row) for row in grid)
8+
9+
# Try horizontal cuts
10+
row_prefix_sum = 0
11+
for i in range(m - 1): # cut between row i and i+1
12+
row_prefix_sum += sum(grid[i])
13+
if row_prefix_sum * 2 == total_sum:
14+
return True
15+
16+
# Try vertical cuts
17+
col_sums = [0] * n
18+
for i in range(m):
19+
for j in range(n):
20+
col_sums[j] += grid[i][j]
21+
22+
col_prefix_sum = 0
23+
for j in range(n - 1): # cut between column j and j+1
24+
col_prefix_sum += col_sums[j]
25+
if col_prefix_sum * 2 == total_sum:
26+
return True
27+
28+
return False

0 commit comments

Comments
 (0)