Leetcode 879 Solution
This article provides solution to leetcode question 879 (maximize-distance-to-closest-person)
Access this page by simply typing in "lcs 879" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/maximize-distance-to-closest-person
Solution
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int first_one_pos = -1;
int last_one_pos = -1;
int max_gap = 0;
for (int i = 0; i < seats.size(); i++)
{
if (seats[i] == 1)
{
if (last_one_pos != -1)
max_gap = max(i - last_one_pos - 1, max_gap);
if (first_one_pos == -1)
first_one_pos = i;
last_one_pos = i;
}
}
return max((max_gap - 1) / 2 + 1, max(first_one_pos, (int)seats.size() - last_one_pos - 1));
}
};