Skip to content

[pull] main from doocs:main #310

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

Merged
merged 3 commits into from
Jan 18, 2025
Merged
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
116 changes: 80 additions & 36 deletions solution/0500-0599/0554.Brick Wall/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:哈希表 + 前缀和

我们可以用一个哈希表 $\textit{cnt}$ 记录每一行除了最后一个砖块以外的前缀和,其中键为前缀和的值,值为该前缀和出现的次数。

遍历每一行,对于当前行的每一个砖块,我们将其加到当前的前缀和上,然后更新 $\textit{cnt}$。

最后我们遍历 $\textit{cnt}$,找出出现次数最多的前缀和,这就是穿过的砖块数量最少的情况。最后答案即为砖墙的行数减去穿过的砖块数量。

时间复杂度 $O(m \times n)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别是砖墙的行数和砖墙的砖块数。

<!-- tabs:start -->

Expand All @@ -65,15 +73,13 @@ tags:
```python
class Solution:
def leastBricks(self, wall: List[List[int]]) -> int:
cnt = defaultdict(int)
cnt = Counter()
for row in wall:
width = 0
for brick in row[:-1]:
width += brick
cnt[width] += 1
if not cnt:
return len(wall)
return len(wall) - cnt[max(cnt, key=cnt.get)]
s = 0
for x in row[:-1]:
s += x
cnt[s] += 1
return len(wall) - max(cnt.values(), default=0)
```

#### Java
Expand All @@ -82,38 +88,79 @@ class Solution:
class Solution {
public int leastBricks(List<List<Integer>> wall) {
Map<Integer, Integer> cnt = new HashMap<>();
for (List<Integer> row : wall) {
int width = 0;
for (int i = 0, n = row.size() - 1; i < n; i++) {
width += row.get(i);
cnt.merge(width, 1, Integer::sum);
for (var row : wall) {
int s = 0;
for (int i = 0; i + 1 < row.size(); ++i) {
s += row.get(i);
cnt.merge(s, 1, Integer::sum);
}
}
int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0);
return wall.size() - max;
int mx = 0;
for (var x : cnt.values()) {
mx = Math.max(mx, x);
}
return wall.size() - mx;
}
}
```

#### C++

```cpp
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
unordered_map<int, int> cnt;
for (const auto& row : wall) {
int s = 0;
for (int i = 0; i + 1 < row.size(); ++i) {
s += row[i];
cnt[s]++;
}
}
int mx = 0;
for (const auto& [_, x] : cnt) {
mx = max(mx, x);
}
return wall.size() - mx;
}
};
```

#### Go

```go
func leastBricks(wall [][]int) int {
cnt := make(map[int]int)
cnt := map[int]int{}
for _, row := range wall {
width := 0
for _, brick := range row[:len(row)-1] {
width += brick
cnt[width]++
s := 0
for _, x := range row[:len(row)-1] {
s += x
cnt[s]++
}
}
max := 0
for _, v := range cnt {
if v > max {
max = v
}
mx := 0
for _, x := range cnt {
mx = max(mx, x)
}
return len(wall) - max
return len(wall) - mx
}
```

#### TypeScript

```ts
function leastBricks(wall: number[][]): number {
const cnt: Map<number, number> = new Map();
for (const row of wall) {
let s = 0;
for (let i = 0; i + 1 < row.length; ++i) {
s += row[i];
cnt.set(s, (cnt.get(s) || 0) + 1);
}
}
const mx = Math.max(...cnt.values(), 0);
return wall.length - mx;
}
```

Expand All @@ -127,17 +174,14 @@ func leastBricks(wall [][]int) int {
var leastBricks = function (wall) {
const cnt = new Map();
for (const row of wall) {
let width = 0;
for (let i = 0, n = row.length - 1; i < n; ++i) {
width += row[i];
cnt.set(width, (cnt.get(width) || 0) + 1);
let s = 0;
for (let i = 0; i + 1 < row.length; ++i) {
s += row[i];
cnt.set(s, (cnt.get(s) || 0) + 1);
}
}
let max = 0;
for (const v of cnt.values()) {
max = Math.max(max, v);
}
return wall.length - max;
const mx = Math.max(...cnt.values(), 0);
return wall.length - mx;
};
```

Expand Down
116 changes: 80 additions & 36 deletions solution/0500-0599/0554.Brick Wall/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table + Prefix Sum

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.

Traverse each row, and for each brick in the current row, add it to the current prefix sum and update $\textit{cnt}$.

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.

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.

<!-- tabs:start -->

Expand All @@ -65,15 +73,13 @@ tags:
```python
class Solution:
def leastBricks(self, wall: List[List[int]]) -> int:
cnt = defaultdict(int)
cnt = Counter()
for row in wall:
width = 0
for brick in row[:-1]:
width += brick
cnt[width] += 1
if not cnt:
return len(wall)
return len(wall) - cnt[max(cnt, key=cnt.get)]
s = 0
for x in row[:-1]:
s += x
cnt[s] += 1
return len(wall) - max(cnt.values(), default=0)
```

#### Java
Expand All @@ -82,38 +88,79 @@ class Solution:
class Solution {
public int leastBricks(List<List<Integer>> wall) {
Map<Integer, Integer> cnt = new HashMap<>();
for (List<Integer> row : wall) {
int width = 0;
for (int i = 0, n = row.size() - 1; i < n; i++) {
width += row.get(i);
cnt.merge(width, 1, Integer::sum);
for (var row : wall) {
int s = 0;
for (int i = 0; i + 1 < row.size(); ++i) {
s += row.get(i);
cnt.merge(s, 1, Integer::sum);
}
}
int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0);
return wall.size() - max;
int mx = 0;
for (var x : cnt.values()) {
mx = Math.max(mx, x);
}
return wall.size() - mx;
}
}
```

#### C++

```cpp
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
unordered_map<int, int> cnt;
for (const auto& row : wall) {
int s = 0;
for (int i = 0; i + 1 < row.size(); ++i) {
s += row[i];
cnt[s]++;
}
}
int mx = 0;
for (const auto& [_, x] : cnt) {
mx = max(mx, x);
}
return wall.size() - mx;
}
};
```

#### Go

```go
func leastBricks(wall [][]int) int {
cnt := make(map[int]int)
cnt := map[int]int{}
for _, row := range wall {
width := 0
for _, brick := range row[:len(row)-1] {
width += brick
cnt[width]++
s := 0
for _, x := range row[:len(row)-1] {
s += x
cnt[s]++
}
}
max := 0
for _, v := range cnt {
if v > max {
max = v
}
mx := 0
for _, x := range cnt {
mx = max(mx, x)
}
return len(wall) - max
return len(wall) - mx
}
```

#### TypeScript

```ts
function leastBricks(wall: number[][]): number {
const cnt: Map<number, number> = new Map();
for (const row of wall) {
let s = 0;
for (let i = 0; i + 1 < row.length; ++i) {
s += row[i];
cnt.set(s, (cnt.get(s) || 0) + 1);
}
}
const mx = Math.max(...cnt.values(), 0);
return wall.length - mx;
}
```

Expand All @@ -127,17 +174,14 @@ func leastBricks(wall [][]int) int {
var leastBricks = function (wall) {
const cnt = new Map();
for (const row of wall) {
let width = 0;
for (let i = 0, n = row.length - 1; i < n; ++i) {
width += row[i];
cnt.set(width, (cnt.get(width) || 0) + 1);
let s = 0;
for (let i = 0; i + 1 < row.length; ++i) {
s += row[i];
cnt.set(s, (cnt.get(s) || 0) + 1);
}
}
let max = 0;
for (const v of cnt.values()) {
max = Math.max(max, v);
}
return wall.length - max;
const mx = Math.max(...cnt.values(), 0);
return wall.length - mx;
};
```

Expand Down
18 changes: 18 additions & 0 deletions solution/0500-0599/0554.Brick Wall/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
unordered_map<int, int> cnt;
for (const auto& row : wall) {
int s = 0;
for (int i = 0; i + 1 < row.size(); ++i) {
s += row[i];
cnt[s]++;
}
}
int mx = 0;
for (const auto& [_, x] : cnt) {
mx = max(mx, x);
}
return wall.size() - mx;
}
};
22 changes: 10 additions & 12 deletions solution/0500-0599/0554.Brick Wall/Solution.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
func leastBricks(wall [][]int) int {
cnt := make(map[int]int)
cnt := map[int]int{}
for _, row := range wall {
width := 0
for _, brick := range row[:len(row)-1] {
width += brick
cnt[width]++
s := 0
for _, x := range row[:len(row)-1] {
s += x
cnt[s]++
}
}
max := 0
for _, v := range cnt {
if v > max {
max = v
}
mx := 0
for _, x := range cnt {
mx = max(mx, x)
}
return len(wall) - max
}
return len(wall) - mx
}
19 changes: 11 additions & 8 deletions solution/0500-0599/0554.Brick Wall/Solution.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
class Solution {
public int leastBricks(List<List<Integer>> wall) {
Map<Integer, Integer> cnt = new HashMap<>();
for (List<Integer> row : wall) {
int width = 0;
for (int i = 0, n = row.size() - 1; i < n; i++) {
width += row.get(i);
cnt.merge(width, 1, Integer::sum);
for (var row : wall) {
int s = 0;
for (int i = 0; i + 1 < row.size(); ++i) {
s += row.get(i);
cnt.merge(s, 1, Integer::sum);
}
}
int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0);
return wall.size() - max;
int mx = 0;
for (var x : cnt.values()) {
mx = Math.max(mx, x);
}
return wall.size() - mx;
}
}
}
Loading