-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path554.brick-wall.java
More file actions
54 lines (46 loc) · 1.48 KB
/
Copy path554.brick-wall.java
File metadata and controls
54 lines (46 loc) · 1.48 KB
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
class Solution {
public int leastBricks(List<List<Integer>> wall) {
if(wall.size() == 0)
return 0;
// int res =0;
HashSet[] hsts = new HashSet[wall.size()];
HashSet<Integer> all = new HashSet<>();
int allsum =0;
int sum =0;
int maxs = Integer.MIN_VALUE;
for(int i=0;i<wall.size();i++){
sum =0;
hsts[i] = new HashSet<Integer>();
List<Integer> currw = wall.get(i);
if(currw.size() > 1)
all.add(currw.get(0));
hsts[i].add(currw.get(0));
for(int j=1;j<currw.size();j++){
currw.set(j, currw.get(j-1)+currw.get(j));
hsts[i].add(currw.get(j));
if(j+1!=currw.size())
all.add(currw.get(j));
}
maxs = Math.max(maxs, currw.size());
}
int i=0;
int res = Integer.MAX_VALUE;
for(int cv: all){
// int cv = wall.get(0).get(v);
int l =0;
int tres =0;
while(l<wall.size()){
if(!hsts[l].contains(cv))
tres++;
l++;
}
// if(v +1 != wall.get(0).size()){
res = Math.min(res, tres);
// System.out.println(cv+" "+tres);
// }
}
if(maxs == 1)
return wall.size();
return res;
}
}