Skip to content

Commit 356cd7b

Browse files
authored
feat: add solutions to lc problem: No.3546 (#4402)
1 parent c85ac1d commit 356cd7b

File tree

7 files changed

+457
-8
lines changed

7 files changed

+457
-8
lines changed

solution/3500-3599/3546.Equal Sum Grid Partition I/README.md

+158-4
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,186 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3546.Eq
7070

7171
<!-- solution:start -->
7272

73-
### 方法一
73+
### 方法一:枚举 + 前缀和
74+
75+
我们先计算矩阵中所有元素的和,记为 $s$。如果 $s$ 是奇数,则不可能将矩阵分割成两个和相等的部分,直接返回 `false`
76+
77+
如果 $s$ 是偶数,我们可以枚举所有可能的分割线,判断是否存在一条分割线将矩阵分割成两个和相等的部分。
78+
79+
我们从上到下遍历每一行,计算当前行之前所有行的元素之和 $\textit{pre}$,如果 $\textit{pre} \times 2 = s$,且当前行不是最后一行,则说明可以在当前行和下一行之间进行水平分割,返回 `true`
80+
81+
如果没有找到这样的分割线,我们再从左到右遍历每一列,计算当前列之前所有列的元素之和 $\textit{pre}$,如果 $\textit{pre} \times 2 = s$,且当前列不是最后一列,则说明可以在当前列和下一列之间进行垂直分割,返回 `true`
82+
83+
如果没有找到这样的分割线,则返回 `false`
84+
85+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$,只使用了常数级别的额外空间。
7486

7587
<!-- tabs:start -->
7688

7789
#### Python3
7890

7991
```python
80-
92+
class Solution:
93+
def canPartitionGrid(self, grid: List[List[int]]) -> bool:
94+
s = sum(sum(row) for row in grid)
95+
if s % 2:
96+
return False
97+
pre = 0
98+
for i, row in enumerate(grid):
99+
pre += sum(row)
100+
if pre * 2 == s and i != len(grid) - 1:
101+
return True
102+
pre = 0
103+
for j, col in enumerate(zip(*grid)):
104+
pre += sum(col)
105+
if pre * 2 == s and j != len(grid[0]) - 1:
106+
return True
107+
return False
81108
```
82109

83110
#### Java
84111

85112
```java
86-
113+
class Solution {
114+
public boolean canPartitionGrid(int[][] grid) {
115+
long s = 0;
116+
for (var row : grid) {
117+
for (int x : row) {
118+
s += x;
119+
}
120+
}
121+
if (s % 2 != 0) {
122+
return false;
123+
}
124+
int m = grid.length, n = grid[0].length;
125+
long pre = 0;
126+
for (int i = 0; i < m; ++i) {
127+
for (int x : grid[i]) {
128+
pre += x;
129+
}
130+
if (pre * 2 == s && i < m - 1) {
131+
return true;
132+
}
133+
}
134+
pre = 0;
135+
for (int j = 0; j < n; ++j) {
136+
for (int i = 0; i < m; ++i) {
137+
pre += grid[i][j];
138+
}
139+
if (pre * 2 == s && j < n - 1) {
140+
return true;
141+
}
142+
}
143+
return false;
144+
}
145+
}
87146
```
88147

89148
#### C++
90149

91150
```cpp
92-
151+
class Solution {
152+
public:
153+
bool canPartitionGrid(vector<vector<int>>& grid) {
154+
long long s = 0;
155+
for (const auto& row : grid) {
156+
for (int x : row) {
157+
s += x;
158+
}
159+
}
160+
if (s % 2 != 0) {
161+
return false;
162+
}
163+
int m = grid.size(), n = grid[0].size();
164+
long long pre = 0;
165+
for (int i = 0; i < m; ++i) {
166+
for (int x : grid[i]) {
167+
pre += x;
168+
}
169+
if (pre * 2 == s && i + 1 < m) {
170+
return true;
171+
}
172+
}
173+
pre = 0;
174+
for (int j = 0; j < n; ++j) {
175+
for (int i = 0; i < m; ++i) {
176+
pre += grid[i][j];
177+
}
178+
if (pre * 2 == s && j + 1 < n) {
179+
return true;
180+
}
181+
}
182+
return false;
183+
}
184+
};
93185
```
94186
95187
#### Go
96188
97189
```go
190+
func canPartitionGrid(grid [][]int) bool {
191+
s := 0
192+
for _, row := range grid {
193+
for _, x := range row {
194+
s += x
195+
}
196+
}
197+
if s%2 != 0 {
198+
return false
199+
}
200+
m, n := len(grid), len(grid[0])
201+
pre := 0
202+
for i, row := range grid {
203+
for _, x := range row {
204+
pre += x
205+
}
206+
if pre*2 == s && i+1 < m {
207+
return true
208+
}
209+
}
210+
pre = 0
211+
for j := 0; j < n; j++ {
212+
for i := 0; i < m; i++ {
213+
pre += grid[i][j]
214+
}
215+
if pre*2 == s && j+1 < n {
216+
return true
217+
}
218+
}
219+
return false
220+
}
221+
```
98222

223+
#### TypeScript
224+
225+
```ts
226+
function canPartitionGrid(grid: number[][]): boolean {
227+
let s = 0;
228+
for (const row of grid) {
229+
s += row.reduce((a, b) => a + b, 0);
230+
}
231+
if (s % 2 !== 0) {
232+
return false;
233+
}
234+
const [m, n] = [grid.length, grid[0].length];
235+
let pre = 0;
236+
for (let i = 0; i < m; ++i) {
237+
pre += grid[i].reduce((a, b) => a + b, 0);
238+
if (pre * 2 === s && i + 1 < m) {
239+
return true;
240+
}
241+
}
242+
pre = 0;
243+
for (let j = 0; j < n; ++j) {
244+
for (let i = 0; i < m; ++i) {
245+
pre += grid[i][j];
246+
}
247+
if (pre * 2 === s && j + 1 < n) {
248+
return true;
249+
}
250+
}
251+
return false;
252+
}
99253
```
100254

101255
<!-- tabs:end -->

solution/3500-3599/3546.Equal Sum Grid Partition I/README_EN.md

+158-4
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,186 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3546.Eq
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Enumeration + Prefix Sum
70+
71+
First, we calculate the sum of all elements in the matrix, denoted as $s$. If $s$ is odd, it is impossible to divide the matrix into two parts with equal sums, so we directly return `false`.
72+
73+
If $s$ is even, we can enumerate all possible partition lines to check if there exists a line that divides the matrix into two parts with equal sums.
74+
75+
We traverse each row from top to bottom, calculating the sum of all elements in the rows above the current row, denoted as $\textit{pre}$. If $\textit{pre} \times 2 = s$ and the current row is not the last row, it means we can perform a horizontal partition between the current row and the next row, so we return `true`.
76+
77+
If no such partition line is found, we traverse each column from left to right, calculating the sum of all elements in the columns to the left of the current column, denoted as $\textit{pre}$. If $\textit{pre} \times 2 = s$ and the current column is not the last column, it means we can perform a vertical partition between the current column and the next column, so we return `true`.
78+
79+
If no such partition line is found, we return `false`.
80+
81+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively. The space complexity is $O(1)$, as only constant extra space is used.
7082

7183
<!-- tabs:start -->
7284

7385
#### Python3
7486

7587
```python
76-
88+
class Solution:
89+
def canPartitionGrid(self, grid: List[List[int]]) -> bool:
90+
s = sum(sum(row) for row in grid)
91+
if s % 2:
92+
return False
93+
pre = 0
94+
for i, row in enumerate(grid):
95+
pre += sum(row)
96+
if pre * 2 == s and i != len(grid) - 1:
97+
return True
98+
pre = 0
99+
for j, col in enumerate(zip(*grid)):
100+
pre += sum(col)
101+
if pre * 2 == s and j != len(grid[0]) - 1:
102+
return True
103+
return False
77104
```
78105

79106
#### Java
80107

81108
```java
82-
109+
class Solution {
110+
public boolean canPartitionGrid(int[][] grid) {
111+
long s = 0;
112+
for (var row : grid) {
113+
for (int x : row) {
114+
s += x;
115+
}
116+
}
117+
if (s % 2 != 0) {
118+
return false;
119+
}
120+
int m = grid.length, n = grid[0].length;
121+
long pre = 0;
122+
for (int i = 0; i < m; ++i) {
123+
for (int x : grid[i]) {
124+
pre += x;
125+
}
126+
if (pre * 2 == s && i < m - 1) {
127+
return true;
128+
}
129+
}
130+
pre = 0;
131+
for (int j = 0; j < n; ++j) {
132+
for (int i = 0; i < m; ++i) {
133+
pre += grid[i][j];
134+
}
135+
if (pre * 2 == s && j < n - 1) {
136+
return true;
137+
}
138+
}
139+
return false;
140+
}
141+
}
83142
```
84143

85144
#### C++
86145

87146
```cpp
88-
147+
class Solution {
148+
public:
149+
bool canPartitionGrid(vector<vector<int>>& grid) {
150+
long long s = 0;
151+
for (const auto& row : grid) {
152+
for (int x : row) {
153+
s += x;
154+
}
155+
}
156+
if (s % 2 != 0) {
157+
return false;
158+
}
159+
int m = grid.size(), n = grid[0].size();
160+
long long pre = 0;
161+
for (int i = 0; i < m; ++i) {
162+
for (int x : grid[i]) {
163+
pre += x;
164+
}
165+
if (pre * 2 == s && i + 1 < m) {
166+
return true;
167+
}
168+
}
169+
pre = 0;
170+
for (int j = 0; j < n; ++j) {
171+
for (int i = 0; i < m; ++i) {
172+
pre += grid[i][j];
173+
}
174+
if (pre * 2 == s && j + 1 < n) {
175+
return true;
176+
}
177+
}
178+
return false;
179+
}
180+
};
89181
```
90182
91183
#### Go
92184
93185
```go
186+
func canPartitionGrid(grid [][]int) bool {
187+
s := 0
188+
for _, row := range grid {
189+
for _, x := range row {
190+
s += x
191+
}
192+
}
193+
if s%2 != 0 {
194+
return false
195+
}
196+
m, n := len(grid), len(grid[0])
197+
pre := 0
198+
for i, row := range grid {
199+
for _, x := range row {
200+
pre += x
201+
}
202+
if pre*2 == s && i+1 < m {
203+
return true
204+
}
205+
}
206+
pre = 0
207+
for j := 0; j < n; j++ {
208+
for i := 0; i < m; i++ {
209+
pre += grid[i][j]
210+
}
211+
if pre*2 == s && j+1 < n {
212+
return true
213+
}
214+
}
215+
return false
216+
}
217+
```
94218

219+
#### TypeScript
220+
221+
```ts
222+
function canPartitionGrid(grid: number[][]): boolean {
223+
let s = 0;
224+
for (const row of grid) {
225+
s += row.reduce((a, b) => a + b, 0);
226+
}
227+
if (s % 2 !== 0) {
228+
return false;
229+
}
230+
const [m, n] = [grid.length, grid[0].length];
231+
let pre = 0;
232+
for (let i = 0; i < m; ++i) {
233+
pre += grid[i].reduce((a, b) => a + b, 0);
234+
if (pre * 2 === s && i + 1 < m) {
235+
return true;
236+
}
237+
}
238+
pre = 0;
239+
for (let j = 0; j < n; ++j) {
240+
for (let i = 0; i < m; ++i) {
241+
pre += grid[i][j];
242+
}
243+
if (pre * 2 === s && j + 1 < n) {
244+
return true;
245+
}
246+
}
247+
return false;
248+
}
95249
```
96250

97251
<!-- tabs:end -->

0 commit comments

Comments
 (0)