-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-849.java
More file actions
30 lines (27 loc) · 897 Bytes
/
lc-849.java
File metadata and controls
30 lines (27 loc) · 897 Bytes
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
class Solution {
public int maxDistToClosest(int[] seats) {
if(seats.length == 0) return 0;
if(seats.length == 1) return 1;
if(seats.length == 2) return 1;
List<Integer> ps = new ArrayList();
int max = 0;
for(int i = 0; i<seats.length; i++) {
if(seats[i] == 1) {
if(!ps.isEmpty()) {
int lasti = ps.get(ps.size()-1);
max = Math.max(max, (i-lasti)/2);
}else {
max = Math.max(max, i);
}
ps.add(i);
}else if(i == seats.length-1) {
if(!ps.isEmpty()) {
max = Math.max(max, i - ps.get(ps.size()-1));
}else {
max = Math.max(max, i);
}
}
}
return max;
}
}