-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path849.java
More file actions
56 lines (51 loc) · 1.68 KB
/
849.java
File metadata and controls
56 lines (51 loc) · 1.68 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
55
56
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int maxDistToClosest(int[] seats) {
int best = 0;
int i=0;
while(i<seats.length){
if(seats[i]==0){
int j = i+1;
while(j<seats.length && seats[j]==0)
j++;
if(j==seats.length || i==0)
best = Math.max(best,j-i);
else
best = Math.max(best,(j-i+1)/2);
i=j;
}
i++;
}
return best;
}
}
__________________________________________________________________________________________________
sample 37236 kb submission
class Solution {
public int maxDistToClosest(int[] seats) {
int prev = -1;
int[] max = null;
Function<int[], Integer> dist = (r -> {
int d = r[1] - r[0] - 1;
if (r[0] == -1 || r[1] == seats.length) d *= 2;
return d;
});
for (int i = 0; i < seats.length; i++) {
int s = seats[i];
int[] tmp = null;
if (s == 1) {
tmp = new int[] {prev, i};
prev = i;
} else if (i == seats.length - 1) {
tmp = new int[] {prev, seats.length};
}
if (tmp == null) continue;
if (max == null || dist.apply(tmp) > dist.apply(max)) max = tmp;
}
// System.out.println(Arrays.toString(max));
if (max[0] == -1 || max[1] == seats.length) return max[1] - max[0] - 1;
return (max[1] - max[0]) / 2;
}
}
__________________________________________________________________________________________________