849. Maximize Distance to Closest Person ——weekly contest 87

849. Maximize Distance to Closest Person

题目链接:https://leetcode.com/problems/maximize-distance-to-closest-person/description/

思路:pre[i]存放i之前离最近的1的距离。post记录之后的。 res = max(min(pre[i],[post[i]))

注意点:初始nst需要设计极大或极小值。

 

 1 int maxDistToClosest(vector<int>& seats) {
 2         vector<int> pre,post;
 3         int n = seats.size();
 4         pre.assign(n,0);
 5         post.assign(n,0);
 6         int nst = -200000;
 7         for(int i = 0; i < n; i++){
 8             if(seats[i] == 1){
 9                 nst = i;
10             }else{
11                 pre[i] = i - nst;
12             }
13         }
14         nst = 200000;
15         for(int i = n - 1; i >= 0; i--){
16             if(seats[i]==1){
17                 nst = i;
18             }else{
19                 post[i] = nst - i;
20             }
21         }
22         int res = 0;
23         for(int i = 0; i<n; i++ ){
24             int temp = min(pre[i],post[i]);
25             res = max(res,temp);
26         }
27         return res;
28     }

 

转载于:https://www.cnblogs.com/jinjin-2018/p/9165164.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值