Skip to content

Commit b37dcbf

Browse files
authoredJun 10, 2018
Create maximize-distance-to-closest-person.cpp
1 parent 639617a commit b37dcbf

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int maxDistToClosest(vector<int>& seats) {
7+
int prev = -1, result = 1;
8+
for (int i = 0; i < seats.size(); ++i) {
9+
if (seats[i]) {
10+
if (prev < 0) {
11+
result = i;
12+
} else {
13+
result = max(result, (i - prev) / 2);
14+
}
15+
prev = i;
16+
}
17+
}
18+
return max(result, static_cast<int>(seats.size()) - 1 - prev);
19+
}
20+
};

0 commit comments

Comments
 (0)
Please sign in to comment.