Skip to content

Commit 678bc75

Browse files
committed
Add solution 0554
1 parent 86856f6 commit 678bc75

25 files changed

+559
-334
lines changed

README.md

Lines changed: 240 additions & 235 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
func leastBricks(wall [][]int) int {
4+
m := make(map[int]int)
5+
for _, row := range wall {
6+
sum := 0
7+
for i := 0; i < len(row)-1; i++ {
8+
sum += row[i]
9+
m[sum]++
10+
}
11+
}
12+
max := 0
13+
for _, v := range m {
14+
if v > max {
15+
max = v
16+
}
17+
}
18+
return len(wall) - max
19+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question554 struct {
9+
para554
10+
ans554
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para554 struct {
16+
wall [][]int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans554 struct {
22+
one int
23+
}
24+
25+
func Test_Problem554(t *testing.T) {
26+
27+
qs := []question554{
28+
29+
{
30+
para554{[][]int{{1, 2, 2, 1}, {3, 1, 2}, {1, 3, 2}, {2, 4}, {3, 1, 2}, {1, 3, 1, 1}}},
31+
ans554{2},
32+
},
33+
34+
{
35+
para554{[][]int{{1}, {1}, {1}}},
36+
ans554{3},
37+
},
38+
39+
{
40+
para554{[][]int{{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}},
41+
ans554{1},
42+
},
43+
44+
{
45+
para554{[][]int{{1, 1, 0}, {1, 1, 1}, {0, 1, 1}}},
46+
ans554{0},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 554------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans554, q.para554
54+
fmt.Printf("【input】:%v 【output】:%v\n", p, leastBricks(p.wall))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}

leetcode/0554.Brick-Wall/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# [554. Brick Wall](https://leetcode.com/problems/brick-wall/)
2+
3+
## 题目
4+
5+
There is a rectangular brick wall in front of you with `n` rows of bricks. The `ith` row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.
6+
7+
Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.
8+
9+
Given the 2D array `wall` that contains the information about the wall, return *the minimum number of crossed bricks after drawing such a vertical line*.
10+
11+
**Example 1:**
12+
13+
![https://assets.leetcode.com/uploads/2021/04/24/cutwall-grid.jpg](https://assets.leetcode.com/uploads/2021/04/24/cutwall-grid.jpg)
14+
15+
```
16+
Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
17+
Output: 2
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: wall = [[1],[1],[1]]
25+
Output: 3
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- `n == wall.length`
32+
- `1 <= n <= 10^4`
33+
- `1 <= wall[i].length <= 10^4`
34+
- `1 <= sum(wall[i].length) <= 2 * 10^4`
35+
- `sum(wall[i])` is the same for each row `i`.
36+
- `1 <= wall[i][j] <= 2^31 - 1`
37+
38+
## 题目大意
39+
40+
你的面前有一堵矩形的、由 n 行砖块组成的砖墙。这些砖块高度相同(也就是一个单位高)但是宽度不同。每一行砖块的宽度之和应该相等。你现在要画一条 自顶向下 的、穿过 最少 砖块的垂线。如果你画的线只是从砖块的边缘经过,就不算穿过这块砖。你不能沿着墙的两个垂直边缘之一画线,这样显然是没有穿过一块砖的。给你一个二维数组 wall ,该数组包含这堵墙的相关信息。其中,wall[i] 是一个代表从左至右每块砖的宽度的数组。你需要找出怎样画才能使这条线 穿过的砖块数量最少 ,并且返回 穿过的砖块数量 。
41+
42+
## 解题思路
43+
44+
- 既然穿过砖块中缝不算穿过砖块,那么穿过最少砖块数量一定是穿过很多中缝。按行遍历每一行的砖块,累加每行砖块宽度,将每行砖块“缝”的坐标存在 map 中。最后取出 map 中出现频次最高的缝,即为铅垂线要穿过的地方。墙高减去缝出现的频次,剩下的即为穿过砖块的数量。
45+
46+
## 代码
47+
48+
```go
49+
package leetcode
50+
51+
func leastBricks(wall [][]int) int {
52+
m := make(map[int]int)
53+
for _, row := range wall {
54+
sum := 0
55+
for i := 0; i < len(row)-1; i++ {
56+
sum += row[i]
57+
m[sum]++
58+
}
59+
}
60+
max := 0
61+
for _, v := range m {
62+
if v > max {
63+
max = v
64+
}
65+
}
66+
return len(wall) - max
67+
}
68+
```

website/content/ChapterFour/0500~0599/0547.Number-of-Provinces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,5 @@ func dfs547(M [][]int, cur int, visited []bool) {
114114
----------------------------------------------
115115
<div style="display: flex;justify-content: space-between;align-items: center;">
116116
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0542.01-Matrix/">⬅️上一页</a></p>
117-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0557.Reverse-Words-in-a-String-III/">下一页➡️</a></p>
117+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0554.Brick-Wall/">下一页➡️</a></p>
118118
</div>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [554. Brick Wall](https://leetcode.com/problems/brick-wall/)
2+
3+
## 题目
4+
5+
There is a rectangular brick wall in front of you with `n` rows of bricks. The `ith` row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.
6+
7+
Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.
8+
9+
Given the 2D array `wall` that contains the information about the wall, return *the minimum number of crossed bricks after drawing such a vertical line*.
10+
11+
**Example 1:**
12+
13+
![https://assets.leetcode.com/uploads/2021/04/24/cutwall-grid.jpg](https://assets.leetcode.com/uploads/2021/04/24/cutwall-grid.jpg)
14+
15+
```
16+
Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
17+
Output: 2
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: wall = [[1],[1],[1]]
25+
Output: 3
26+
27+
```
28+
29+
**Constraints:**
30+
31+
- `n == wall.length`
32+
- `1 <= n <= 10^4`
33+
- `1 <= wall[i].length <= 10^4`
34+
- `1 <= sum(wall[i].length) <= 2 * 10^4`
35+
- `sum(wall[i])` is the same for each row `i`.
36+
- `1 <= wall[i][j] <= 2^31 - 1`
37+
38+
## 题目大意
39+
40+
你的面前有一堵矩形的、由 n 行砖块组成的砖墙。这些砖块高度相同(也就是一个单位高)但是宽度不同。每一行砖块的宽度之和应该相等。你现在要画一条 自顶向下 的、穿过 最少 砖块的垂线。如果你画的线只是从砖块的边缘经过,就不算穿过这块砖。你不能沿着墙的两个垂直边缘之一画线,这样显然是没有穿过一块砖的。给你一个二维数组 wall ,该数组包含这堵墙的相关信息。其中,wall[i] 是一个代表从左至右每块砖的宽度的数组。你需要找出怎样画才能使这条线 穿过的砖块数量最少 ,并且返回 穿过的砖块数量 。
41+
42+
## 解题思路
43+
44+
- 既然穿过砖块中缝不算穿过砖块,那么穿过最少砖块数量一定是穿过很多中缝。按行遍历每一行的砖块,累加每行砖块宽度,将每行砖块“缝”的坐标存在 map 中。最后取出 map 中出现频次最高的缝,即为铅垂线要穿过的地方。墙高减去缝出现的频次,剩下的即为穿过砖块的数量。
45+
46+
## 代码
47+
48+
```go
49+
package leetcode
50+
51+
func leastBricks(wall [][]int) int {
52+
m := make(map[int]int)
53+
for _, row := range wall {
54+
sum := 0
55+
for i := 0; i < len(row)-1; i++ {
56+
sum += row[i]
57+
m[sum]++
58+
}
59+
}
60+
max := 0
61+
for _, v := range m {
62+
if v > max {
63+
max = v
64+
}
65+
}
66+
return len(wall) - max
67+
}
68+
```
69+
70+
71+
----------------------------------------------
72+
<div style="display: flex;justify-content: space-between;align-items: center;">
73+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0547.Number-of-Provinces/">⬅️上一页</a></p>
74+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0557.Reverse-Words-in-a-String-III/">下一页➡️</a></p>
75+
</div>

website/content/ChapterFour/0500~0599/0557.Reverse-Words-in-a-String-III.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ func revers(s string) string {
5959

6060
----------------------------------------------
6161
<div style="display: flex;justify-content: space-between;align-items: center;">
62-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0547.Number-of-Provinces/">⬅️上一页</a></p>
62+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0554.Brick-Wall/">⬅️上一页</a></p>
6363
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0561.Array-Partition-I/">下一页➡️</a></p>
6464
</div>

0 commit comments

Comments
 (0)