-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path849.cpp
35 lines (35 loc) · 999 Bytes
/
849.cpp
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
class Solution {
public:
int maxDistToClosest(vector<int> &seats) {
int maxDist = 1;
int i = 0;
if (seats[i] == 0) {
while (i < seats.size() && seats[i] == 0)
++i;
}
maxDist = maxDist < i ? i : maxDist;
i = seats.size() - 1;
int j = 0;
if(seats[i] == 0) {
while (i >= 0 && seats[i] == 0) {
--i;
++j;
}
}
maxDist = maxDist < j ? j : maxDist;
for (i = 0; i < seats.size(); ++i) {
int current = 1;
if (seats[i] == 0) {
int left = i - 1, right = i + 1;
while (left >= 0 && right < seats.size() && seats[left] == 0 && seats[right] == 0) {
++current;
--left;
++right;
}
}
if (current > maxDist)
maxDist = current;
}
return maxDist;
}
};