Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5aaebae

Browse files
committed
feat: add 554
1 parent685d089 commit5aaebae

File tree

8 files changed

+392
-131
lines changed

8 files changed

+392
-131
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
|3|[Longest Substring Without Repeating Characters][003]|Hash Table, Two Pointers, String|
6161
|8|[String to Integer (atoi)][008]|Math, String|
6262
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
63+
|554|[Brick Wall][554]|Hash Table|
6364

6465

6566
##Hard

‎note/554/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#[Brick Wall][title]
2+
3+
##Description
4+
5+
There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the**top** to the**bottom** and cross the**least** bricks.
6+
7+
The brick wall is represented by a list of rows. Each row is a list of integers representing the width of each brick in this row from left to right.
8+
9+
If your line go through the edge of a brick, then the brick is not considered as crossed. You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.
10+
11+
**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.**
12+
13+
**Example:**
14+
15+
```
16+
Input:
17+
[[1,2,2,1],
18+
[3,1,2],
19+
[1,3,2],
20+
[2,4],
21+
[3,1,2],
22+
[1,3,1,1]]
23+
Output: 2
24+
```
25+
26+
Explanation:
27+
![img](https://leetcode.com/static/images/problemset/brick_wall.png)
28+
29+
**Note:**
30+
31+
1. The width sum of bricks in different rows are the same and won't exceed INT_MAX.
32+
2. The number of bricks in each row is in range[1,10,000]. The height of wall is in range[1,10,000]. Total number of bricks of the wall won't exceed 20,000.
33+
34+
**Tags:** Hash Table
35+
36+
37+
##思路
38+
39+
题意根据图示已经描述得很清楚了,就是在从底部到顶部,求最少交叉的数量,我们可以把每堵墙可以穿过的地方保存到哈希表中,每次遇到哈希表中的值加一,代表就是这条路不用交叉的数量,最终我们可以算出不用交叉的最大值,让总墙数减去其值就是最少交叉的数量。
40+
41+
```java
42+
classSolution {
43+
publicintleastBricks(List<List<Integer>>wall) {
44+
Map<Integer,Integer> map=newHashMap<>();
45+
int width=0, max=0;
46+
for (List<Integer> sub: wall) {
47+
int p=0;
48+
for (int i=0, len= sub.size()-1; i< len;++i) {
49+
p+= sub.get(i);
50+
Integer v= map.get(p);
51+
map.put(p, (v==null?0: v)+1);
52+
}
53+
}
54+
for (Integer integer: map.values()) {
55+
if (integer> max) max= integer;
56+
}
57+
return wall.size()- max;
58+
}
59+
}
60+
```
61+
62+
63+
##结语
64+
65+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
66+
67+
68+
69+
[title]:https://leetcode.com/problems/brick-wall
70+
[ajl]:https://github.com/Blankj/awesome-java-leetcode

‎project/leetcode/.idea/workspace.xml

Lines changed: 271 additions & 93 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
packagecom.blankj.medium._019;
22

3+
importcom.blankj.structure.ListNode;
4+
35
/**
46
* <pre>
57
* author: Blankj
@@ -9,27 +11,6 @@
911
* </pre>
1012
*/
1113
publicclassSolution {
12-
13-
staticclassListNode {
14-
intval;
15-
ListNodenext;
16-
17-
ListNode(intx) {
18-
val =x;
19-
}
20-
21-
@Override
22-
publicStringtoString() {
23-
Stringstr ="[" +String.valueOf(val);
24-
ListNodep =next;
25-
while (p !=null) {
26-
str +=", " +String.valueOf(p.val);
27-
p =p.next;
28-
}
29-
returnstr +"]";
30-
}
31-
}
32-
3314
publicListNoderemoveNthFromEnd(ListNodehead,intn) {
3415
ListNodepre =head;
3516
ListNodeafterPreN =head;
@@ -50,22 +31,7 @@ public ListNode removeNthFromEnd(ListNode head, int n) {
5031

5132
publicstaticvoidmain(String[]args) {
5233
Solutionsolution =newSolution();
53-
ListNodelistNode0 =newListNode(1);
54-
ListNodelistNode1 =newListNode(2);
55-
ListNodelistNode2 =newListNode(3);
56-
ListNodelistNode3 =newListNode(4);
57-
ListNodelistNode4 =newListNode(5);
58-
listNode0.next =listNode1;
59-
listNode1.next =listNode2;
60-
listNode2.next =listNode3;
61-
listNode3.next =listNode4;
62-
listNode4.next =null;
63-
System.out.println(listNode0.toString());
64-
System.out.println(solution.removeNthFromEnd(listNode0,2).toString());
65-
66-
ListNodelistNode5 =newListNode(1);
67-
System.out.println(listNode5.toString());
68-
ListNoderes =solution.removeNthFromEnd(listNode5,1);
69-
System.out.println(res ==null ?"[]" :res.toString());
34+
ListNode.print(solution.removeNthFromEnd(ListNode.createTestData("[1,2,3,4,5]"),2));
35+
ListNode.print(solution.removeNthFromEnd(ListNode.createTestData("[1]"),1));
7036
}
7137
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
packagecom.blankj.medium._554;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.HashMap;
6+
importjava.util.List;
7+
importjava.util.Map;
8+
9+
/**
10+
* <pre>
11+
* author: Blankj
12+
* blog : http://blankj.com
13+
* time : 2017/10/13
14+
* desc :
15+
* </pre>
16+
*/
17+
publicclassSolution {
18+
publicintleastBricks(List<List<Integer>>wall) {
19+
Map<Integer,Integer>map =newHashMap<>();
20+
intwidth =0,max =0;
21+
for (List<Integer>sub :wall) {
22+
intp =0;
23+
for (inti =0,len =sub.size() -1;i <len; ++i) {
24+
p +=sub.get(i);
25+
Integerv =map.get(p);
26+
map.put(p, (v ==null ?0 :v) +1);
27+
}
28+
}
29+
for (Integerinteger :map.values()) {
30+
if (integer >max)max =integer;
31+
}
32+
returnwall.size() -max;
33+
}
34+
35+
publicstaticvoidmain(String[]args) {
36+
Solutionsolution =newSolution();
37+
List<List<Integer>>list =newArrayList<>();
38+
list.add(Arrays.asList(1,2,2,1));
39+
list.add(Arrays.asList(3,1,2));
40+
list.add(Arrays.asList(1,3,2));
41+
list.add(Arrays.asList(2,4));
42+
list.add(Arrays.asList(3,1,2));
43+
list.add(Arrays.asList(1,3,1,1));
44+
System.out.println(solution.leastBricks(list));
45+
}
46+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp