Skip to content

Commit 8da9ea5

Browse files
authored
feat: add solutions to lc problem: No.0554 (#3966)
No.0554.Brick Wall
1 parent ce96174 commit 8da9ea5

File tree

8 files changed

+223
-109
lines changed

8 files changed

+223
-109
lines changed

solution/0500-0599/0554.Brick Wall/README.md

+80-36
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### 方法一
59+
### 方法一:哈希表 + 前缀和
60+
61+
我们可以用一个哈希表 $\textit{cnt}$ 记录每一行除了最后一个砖块以外的前缀和,其中键为前缀和的值,值为该前缀和出现的次数。
62+
63+
遍历每一行,对于当前行的每一个砖块,我们将其加到当前的前缀和上,然后更新 $\textit{cnt}$。
64+
65+
最后我们遍历 $\textit{cnt}$,找出出现次数最多的前缀和,这就是穿过的砖块数量最少的情况。最后答案即为砖墙的行数减去穿过的砖块数量。
66+
67+
时间复杂度 $O(m \times n)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别是砖墙的行数和砖墙的砖块数。
6068

6169
<!-- tabs:start -->
6270

@@ -65,15 +73,13 @@ tags:
6573
```python
6674
class Solution:
6775
def leastBricks(self, wall: List[List[int]]) -> int:
68-
cnt = defaultdict(int)
76+
cnt = Counter()
6977
for row in wall:
70-
width = 0
71-
for brick in row[:-1]:
72-
width += brick
73-
cnt[width] += 1
74-
if not cnt:
75-
return len(wall)
76-
return len(wall) - cnt[max(cnt, key=cnt.get)]
78+
s = 0
79+
for x in row[:-1]:
80+
s += x
81+
cnt[s] += 1
82+
return len(wall) - max(cnt.values(), default=0)
7783
```
7884

7985
#### Java
@@ -82,38 +88,79 @@ class Solution:
8288
class Solution {
8389
public int leastBricks(List<List<Integer>> wall) {
8490
Map<Integer, Integer> cnt = new HashMap<>();
85-
for (List<Integer> row : wall) {
86-
int width = 0;
87-
for (int i = 0, n = row.size() - 1; i < n; i++) {
88-
width += row.get(i);
89-
cnt.merge(width, 1, Integer::sum);
91+
for (var row : wall) {
92+
int s = 0;
93+
for (int i = 0; i + 1 < row.size(); ++i) {
94+
s += row.get(i);
95+
cnt.merge(s, 1, Integer::sum);
9096
}
9197
}
92-
int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0);
93-
return wall.size() - max;
98+
int mx = 0;
99+
for (var x : cnt.values()) {
100+
mx = Math.max(mx, x);
101+
}
102+
return wall.size() - mx;
94103
}
95104
}
96105
```
97106

107+
#### C++
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int leastBricks(vector<vector<int>>& wall) {
113+
unordered_map<int, int> cnt;
114+
for (const auto& row : wall) {
115+
int s = 0;
116+
for (int i = 0; i + 1 < row.size(); ++i) {
117+
s += row[i];
118+
cnt[s]++;
119+
}
120+
}
121+
int mx = 0;
122+
for (const auto& [_, x] : cnt) {
123+
mx = max(mx, x);
124+
}
125+
return wall.size() - mx;
126+
}
127+
};
128+
```
129+
98130
#### Go
99131
100132
```go
101133
func leastBricks(wall [][]int) int {
102-
cnt := make(map[int]int)
134+
cnt := map[int]int{}
103135
for _, row := range wall {
104-
width := 0
105-
for _, brick := range row[:len(row)-1] {
106-
width += brick
107-
cnt[width]++
136+
s := 0
137+
for _, x := range row[:len(row)-1] {
138+
s += x
139+
cnt[s]++
108140
}
109141
}
110-
max := 0
111-
for _, v := range cnt {
112-
if v > max {
113-
max = v
114-
}
142+
mx := 0
143+
for _, x := range cnt {
144+
mx = max(mx, x)
115145
}
116-
return len(wall) - max
146+
return len(wall) - mx
147+
}
148+
```
149+
150+
#### TypeScript
151+
152+
```ts
153+
function leastBricks(wall: number[][]): number {
154+
const cnt: Map<number, number> = new Map();
155+
for (const row of wall) {
156+
let s = 0;
157+
for (let i = 0; i + 1 < row.length; ++i) {
158+
s += row[i];
159+
cnt.set(s, (cnt.get(s) || 0) + 1);
160+
}
161+
}
162+
const mx = Math.max(...cnt.values(), 0);
163+
return wall.length - mx;
117164
}
118165
```
119166

@@ -127,17 +174,14 @@ func leastBricks(wall [][]int) int {
127174
var leastBricks = function (wall) {
128175
const cnt = new Map();
129176
for (const row of wall) {
130-
let width = 0;
131-
for (let i = 0, n = row.length - 1; i < n; ++i) {
132-
width += row[i];
133-
cnt.set(width, (cnt.get(width) || 0) + 1);
177+
let s = 0;
178+
for (let i = 0; i + 1 < row.length; ++i) {
179+
s += row[i];
180+
cnt.set(s, (cnt.get(s) || 0) + 1);
134181
}
135182
}
136-
let max = 0;
137-
for (const v of cnt.values()) {
138-
max = Math.max(max, v);
139-
}
140-
return wall.length - max;
183+
const mx = Math.max(...cnt.values(), 0);
184+
return wall.length - mx;
141185
};
142186
```
143187

solution/0500-0599/0554.Brick Wall/README_EN.md

+80-36
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Hash Table + Prefix Sum
60+
61+
We can use a hash table $\textit{cnt}$ to record the prefix sum of each row except for the last brick. The key is the value of the prefix sum, and the value is the number of times the prefix sum appears.
62+
63+
Traverse each row, and for each brick in the current row, add it to the current prefix sum and update $\textit{cnt}$.
64+
65+
Finally, we traverse $\textit{cnt}$ to find the prefix sum that appears the most times, which represents the situation where the least number of bricks are crossed. The final answer is the number of rows in the brick wall minus the number of bricks crossed.
66+
67+
The time complexity is $O(m \times n)$, and the space complexity is $O(n)$. Here, $m$ and $n$ are the number of rows and the number of bricks in the brick wall, respectively.
6068

6169
<!-- tabs:start -->
6270

@@ -65,15 +73,13 @@ tags:
6573
```python
6674
class Solution:
6775
def leastBricks(self, wall: List[List[int]]) -> int:
68-
cnt = defaultdict(int)
76+
cnt = Counter()
6977
for row in wall:
70-
width = 0
71-
for brick in row[:-1]:
72-
width += brick
73-
cnt[width] += 1
74-
if not cnt:
75-
return len(wall)
76-
return len(wall) - cnt[max(cnt, key=cnt.get)]
78+
s = 0
79+
for x in row[:-1]:
80+
s += x
81+
cnt[s] += 1
82+
return len(wall) - max(cnt.values(), default=0)
7783
```
7884

7985
#### Java
@@ -82,38 +88,79 @@ class Solution:
8288
class Solution {
8389
public int leastBricks(List<List<Integer>> wall) {
8490
Map<Integer, Integer> cnt = new HashMap<>();
85-
for (List<Integer> row : wall) {
86-
int width = 0;
87-
for (int i = 0, n = row.size() - 1; i < n; i++) {
88-
width += row.get(i);
89-
cnt.merge(width, 1, Integer::sum);
91+
for (var row : wall) {
92+
int s = 0;
93+
for (int i = 0; i + 1 < row.size(); ++i) {
94+
s += row.get(i);
95+
cnt.merge(s, 1, Integer::sum);
9096
}
9197
}
92-
int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0);
93-
return wall.size() - max;
98+
int mx = 0;
99+
for (var x : cnt.values()) {
100+
mx = Math.max(mx, x);
101+
}
102+
return wall.size() - mx;
94103
}
95104
}
96105
```
97106

107+
#### C++
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int leastBricks(vector<vector<int>>& wall) {
113+
unordered_map<int, int> cnt;
114+
for (const auto& row : wall) {
115+
int s = 0;
116+
for (int i = 0; i + 1 < row.size(); ++i) {
117+
s += row[i];
118+
cnt[s]++;
119+
}
120+
}
121+
int mx = 0;
122+
for (const auto& [_, x] : cnt) {
123+
mx = max(mx, x);
124+
}
125+
return wall.size() - mx;
126+
}
127+
};
128+
```
129+
98130
#### Go
99131
100132
```go
101133
func leastBricks(wall [][]int) int {
102-
cnt := make(map[int]int)
134+
cnt := map[int]int{}
103135
for _, row := range wall {
104-
width := 0
105-
for _, brick := range row[:len(row)-1] {
106-
width += brick
107-
cnt[width]++
136+
s := 0
137+
for _, x := range row[:len(row)-1] {
138+
s += x
139+
cnt[s]++
108140
}
109141
}
110-
max := 0
111-
for _, v := range cnt {
112-
if v > max {
113-
max = v
114-
}
142+
mx := 0
143+
for _, x := range cnt {
144+
mx = max(mx, x)
115145
}
116-
return len(wall) - max
146+
return len(wall) - mx
147+
}
148+
```
149+
150+
#### TypeScript
151+
152+
```ts
153+
function leastBricks(wall: number[][]): number {
154+
const cnt: Map<number, number> = new Map();
155+
for (const row of wall) {
156+
let s = 0;
157+
for (let i = 0; i + 1 < row.length; ++i) {
158+
s += row[i];
159+
cnt.set(s, (cnt.get(s) || 0) + 1);
160+
}
161+
}
162+
const mx = Math.max(...cnt.values(), 0);
163+
return wall.length - mx;
117164
}
118165
```
119166

@@ -127,17 +174,14 @@ func leastBricks(wall [][]int) int {
127174
var leastBricks = function (wall) {
128175
const cnt = new Map();
129176
for (const row of wall) {
130-
let width = 0;
131-
for (let i = 0, n = row.length - 1; i < n; ++i) {
132-
width += row[i];
133-
cnt.set(width, (cnt.get(width) || 0) + 1);
177+
let s = 0;
178+
for (let i = 0; i + 1 < row.length; ++i) {
179+
s += row[i];
180+
cnt.set(s, (cnt.get(s) || 0) + 1);
134181
}
135182
}
136-
let max = 0;
137-
for (const v of cnt.values()) {
138-
max = Math.max(max, v);
139-
}
140-
return wall.length - max;
183+
const mx = Math.max(...cnt.values(), 0);
184+
return wall.length - mx;
141185
};
142186
```
143187

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int leastBricks(vector<vector<int>>& wall) {
4+
unordered_map<int, int> cnt;
5+
for (const auto& row : wall) {
6+
int s = 0;
7+
for (int i = 0; i + 1 < row.size(); ++i) {
8+
s += row[i];
9+
cnt[s]++;
10+
}
11+
}
12+
int mx = 0;
13+
for (const auto& [_, x] : cnt) {
14+
mx = max(mx, x);
15+
}
16+
return wall.size() - mx;
17+
}
18+
};
+10-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
func leastBricks(wall [][]int) int {
2-
cnt := make(map[int]int)
2+
cnt := map[int]int{}
33
for _, row := range wall {
4-
width := 0
5-
for _, brick := range row[:len(row)-1] {
6-
width += brick
7-
cnt[width]++
4+
s := 0
5+
for _, x := range row[:len(row)-1] {
6+
s += x
7+
cnt[s]++
88
}
99
}
10-
max := 0
11-
for _, v := range cnt {
12-
if v > max {
13-
max = v
14-
}
10+
mx := 0
11+
for _, x := range cnt {
12+
mx = max(mx, x)
1513
}
16-
return len(wall) - max
17-
}
14+
return len(wall) - mx
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
class Solution {
22
public int leastBricks(List<List<Integer>> wall) {
33
Map<Integer, Integer> cnt = new HashMap<>();
4-
for (List<Integer> row : wall) {
5-
int width = 0;
6-
for (int i = 0, n = row.size() - 1; i < n; i++) {
7-
width += row.get(i);
8-
cnt.merge(width, 1, Integer::sum);
4+
for (var row : wall) {
5+
int s = 0;
6+
for (int i = 0; i + 1 < row.size(); ++i) {
7+
s += row.get(i);
8+
cnt.merge(s, 1, Integer::sum);
99
}
1010
}
11-
int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0);
12-
return wall.size() - max;
11+
int mx = 0;
12+
for (var x : cnt.values()) {
13+
mx = Math.max(mx, x);
14+
}
15+
return wall.size() - mx;
1316
}
14-
}
17+
}

0 commit comments

Comments
 (0)