# Leetcode 849. Maximize Distance to Closest Person

## 2. Solution

• Version 1
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int max_distance = 0;
int distance = 0;
int left = -1;
int right = -1;
int size = seats.size();
for(int i = 0; i < seats.size(); i++) {
if(!seats[i]) {
getRight(seats, right, i);
calculateDistance(i, left, right, max_distance, size);
}
else {
left = i;
}
}
return max_distance;
}

private:
void calculateDistance(int current, int left, int& right, int& max_distance, int size) {
int distance_left = INT_MAX;
int distance_right = INT_MAX;
int distance = 0;
if(left > -1) {
distance_left = current - left;
}
if(right < size){
distance_right = right - current;
}
distance = min(distance_left, distance_right);
max_distance = max(max_distance, distance);
}

void getRight(vector<int>& seats, int& right, int current) {
if(right <= current) {
for(int i = current + 1; i < seats.size(); i++) {
if(seats[i]) {
right = i;
break;
}
else {
right = i + 1;
}
}
}
}
};

• Version 2
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int max_distance = 0;
int distance = 0;
int left = -1;
int right = seats.size() - 1;
for(int i = 0; i < seats.size(); i++) {
if(seats[i]) {
if(left == -1) {
max_distance = max(max_distance, i);
}
else {
max_distance = max(max_distance, (i - left) / 2);
}
left = i;
}
}
if(!seats[right]) {
max_distance = max(max_distance, right - left);
}
return max_distance;
}
};


## Reference

08-04 220

10-18 36

06-10 1681

01-15 46

04-26 17

04-21 75

08-12 321

04-14 24

08-08 26

07-10 11

07-19 13

11-20 12

07-13 157

01-01 32

06-11 14

03-19 80万+

04-14 56万+

02-19 16万+

02-27 7万+

02-28 4万+

03-01 12万+

03-01 11万+

03-03 6032

03-04 12万+

03-05 9603

03-05 5万+

03-08 6万+

03-08 1万+

04-25 6万+

03-10 12万+

03-10 17万+

03-10 6258

03-12 10万+

03-13 10万+

03-16 9905

03-19 7万+

03-20 4874

03-23 1万+

03-24 3万+

03-25 2万+

03-25 8万+

03-27 1万+

03-29 20万+

03-29 9万+

03-30 14万+

05-21 2834

03-31 1万+

05-25 5091

03-23 1万+

04-02 3万+

05-06 2万+

04-05 1万+

04-06 6万+

04-09 1万+

04-09 7万+

04-09 4623

04-10 4168

04-11 2万+

04-15 5万+

04-18 1万+

04-18 3709

04-18 4万+

04-20 3万+

04-24 2万+

04-26 3285

04-24 3934

04-30 7024

05-16 4万+

05-08 3万+

05-10 1808

05-11 2万+

05-13 6672

05-19 5592

05-13 9676

05-14 3584

05-14 6057

05-16 1882

05-16 1281

05-16 9294

05-16 1万+

#### boss说了：有谁要再用double定义商品金额，就自己收拾家伙走

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客