-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathBrickWall.java
143 lines (129 loc) · 4.38 KB
/
BrickWall.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package LeetCodeJava.HashTable;
import java.util.*;
// https://leetcode.com/problems/brick-wall/discuss/101728/I-DON'T-THINK-THERE-IS-A-BETTER-PERSON-THAN-ME-TO-ANSWER-THIS-QUESTION
// https://leetcode.ca/2017-06-06-554-Brick-Wall/
/**
* 554. Brick Wall
* Description
* 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.
*
* 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.
*
* 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.
*
*
*
* Example 1:
*
*
*
* Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
* Output: 2
* Example 2:
*
* Input: wall = [[1],[1],[1]]
* Output: 3
*
*
* Constraints:
*
* n == wall.length
* 1 <= n <= 104
* 1 <= wall[i].length <= 104
* 1 <= sum(wall[i].length) <= 2 * 104
* sum(wall[i]) is the same for each row i.
* 1 <= wall[i][j] <= 231 - 1
*
*
*/
public class BrickWall {
// V0
// IDEA: HASH TABLE + SORTING
// TODO: fix
// public int leastBricks(List<List<Integer>> wall) {
//
// // edge
// if (wall.isEmpty()){
// return 0;
// }
//
// int res = 0;
// Map<Integer, Integer> cnt = new HashMap<>();
// // build counter map
// // TODO: optimize double loop
// for(List<Integer> w: wall){
// int cumSum = 0;
// for(Integer x: w){
// cumSum += x;
// int val = cnt.getOrDefault(x, 0);
// cnt.put(x, val+1);
// }
// }
//
// // sort on val
// List<Integer> keys = new ArrayList<>(cnt.keySet());
//
// // edge case : wall are all the same
// if(keys.size() == 1){
// return wall.size();
// }
//
// System.out.println(">>> (before) keys = " + keys);
//
// Collections.sort(keys, new Comparator<Integer>() {
// @Override
// public int compare(Integer o1, Integer o2) {
// // sort on val, decreasing order
// return o2 - o1;
// }
// });
//
// System.out.println(">>> (before) keys = " + keys);
//
// return wall.size() - keys.get(0);
// }
// V0-1
// IDEA: HASH TABLE + SORTING (fixed by gpt)
public int leastBricks_0_1(List<List<Integer>> wall) {
// Edge case: if the wall is empty, return 0
if (wall.isEmpty()) {
return 0;
}
Map<Integer, Integer> cnt = new HashMap<>();
// Loop through each row (wall) to build the counter map
for (List<Integer> w : wall) {
int cumSum = 0;
// Loop through each brick in the current row
for (int i = 0; i < w.size() - 1; i++) { // We don't count the last brick to avoid the edge of the wall
cumSum += w.get(i);
cnt.put(cumSum, cnt.getOrDefault(cumSum, 0) + 1);
}
}
// If cnt is empty, it means there are no gaps, so all bricks are aligned perfectly, and we'd have to cross all rows.
if (cnt.isEmpty()) {
return wall.size();
}
// Find the maximum count of any position
int maxCount = 0;
for (int count : cnt.values()) {
maxCount = Math.max(maxCount, count);
}
// The least number of bricks crossed will be the total number of rows minus the max number of rows that share a common edge
return wall.size() - maxCount;
}
// V1
// https://leetcode.ca/2017-06-06-554-Brick-Wall/
public int leastBricks_1(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);
}
}
int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0);
return wall.size() - max;
}
// V2
}