File tree 1 file changed +28
-1
lines changed
solution/3500-3599/3548.Equal Sum Grid Partition II
1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -115,7 +115,34 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3548.Eq
115
115
#### Python3
116
116
117
117
``` python
118
-
118
+ from typing import List
119
+
120
+ class Solution :
121
+ def canPartitionGrid (self , grid : List[List[int ]]) -> bool :
122
+ m, n = len (grid), len (grid[0 ])
123
+
124
+ total_sum = sum (sum (row) for row in grid)
125
+
126
+ # Try horizontal cuts
127
+ row_prefix_sum = 0
128
+ for i in range (m - 1 ): # cut between row i and i+1
129
+ row_prefix_sum += sum (grid[i])
130
+ if row_prefix_sum * 2 == total_sum:
131
+ return True
132
+
133
+ # Try vertical cuts
134
+ col_sums = [0 ] * n
135
+ for i in range (m):
136
+ for j in range (n):
137
+ col_sums[j] += grid[i][j]
138
+
139
+ col_prefix_sum = 0
140
+ for j in range (n - 1 ): # cut between column j and j+1
141
+ col_prefix_sum += col_sums[j]
142
+ if col_prefix_sum * 2 == total_sum:
143
+ return True
144
+
145
+ return False
119
146
```
120
147
121
148
#### Java
You can’t perform that action at this time.
0 commit comments