-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path849.cpp
More file actions
25 lines (25 loc) · 708 Bytes
/
849.cpp
File metadata and controls
25 lines (25 loc) · 708 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
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int res = 0;
int currLocation = 0;
bool first = true;
for (int i = 0; i < seats.size(); ++i) {
if (seats[i] == 1) {
if (first) {
res = max(res, i);
currLocation = i;
first = false;
}
else {
int diff = i - currLocation;
res = max(res, diff / 2);
currLocation = i;
}
}
}
// last
res = max(res, static_cast<int>(seats.size()) - currLocation - 1);
return res;
}
};