-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
32 lines (29 loc) · 1019 Bytes
/
Solution.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
class Solution {
public int maxDistToClosest(int[] seats) {
int start = 0,
end = 0,
maxDistance = 0,
length = seats.length;
for (int i = 0; i < length; i++) {
int seat = seats[i];
if (seat == 1) {
if (start == 0 || end == length - 1) {
maxDistance = Math.max(maxDistance, end - start + 1);
} else {
maxDistance = Math.max(maxDistance, (end - start + 1) / 2 + (end - start + 1) % 2);
}
if (i + 1 < length) {
start = end = i + 1;
}
} else {
end = i;
}
}
if (start == 0 || end == length - 1) {
maxDistance = Math.max(maxDistance, end - start + 1);
} else {
maxDistance = Math.max(maxDistance, (end - start + 1) / 2 + (end - start + 1) % 2);
}
return maxDistance;
}
}