【Leetcode_easy】849. Maximize Distance to Closest Person

problem

849. Maximize Distance to Closest Person

solution1:

class Solution {
public:
    int maxDistToClosest(vector<int>& seats) {
        int res = 0, n = seats.size();
        vector<int> pos;
        for(int i=0; i<n; ++i)
        {
            if(seats[i] == 1) pos.push_back(i); 
        }
        for(int i=0; i<pos.size(); ++i)
        {
            if(i==0) res = max(res, pos[i]);
            else res = max(res, (pos[i]-pos[i-1])/2);
        }
        if(!pos.empty()) res = max(res, n-1-pos.back());
        return res;
    }
};

 

参考

1. Leetcode_easy_849. Maximize Distance to Closest Person;

2. grandyang;

posted on 2019-07-19 18:00  鹅要长大  阅读(144)  评论(0编辑  收藏  举报

导航