2023-08-22力扣每日一题

链接:

849. 到最近的人的最大距离

题意:

找一个离人最远的位置

解:

每个位置都用左边的人和右边的人更新一遍距离即可

实际代码:

c++ 复制代码
#include<bits/stdc++.h>
using namespace std;
int maxDistToClosest(vector<int>& seats)
{
    int lg=seats.size();
    vector<int>distance(lg,INT_MAX);

    int near=-1;
    for(int i=0;i<lg;i++)
    {
        if(seats[i]) near=i;
        if(near!=-1) distance[i]=min(distance[i],i-near);
    }
    near=-1;int ans=-1;
    for(int i=lg-1;i>=0;i--)
    {
        if(seats[i]) near=i;
        if(near!=-1) distance[i]=min(distance[i],near-i);
        ans=max(ans,distance[i]);
    }
    return ans;
}
int main()
{
    return 0; 
}

限制:

  • 2 <= seats.length <= 2 * 104
  • seats[i] 为 0 或 1
  • 至少有一个 空座位
  • 至少有一个 座位上有人
相关推荐
所以遗憾是什么呢?3 小时前
【题解】Educational Codeforces Round 195 (Rated for Div. 2) ABCDE
c++·动态规划·acm·icpc·ccpc·xcpc·codeforecs
Lhan.zzZ4 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
sysu_chenxy6 小时前
二重循环输出二维图形
c++
Navigator_Z6 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
Nil2087 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
Nil2087 小时前
leetcode 72编辑距离
算法·leetcode·职场和发展
划破黑暗的第一缕曙光7 小时前
[C++]:2.类和对象(下)
c++·类和对象·对象·类
Nil2088 小时前
leetcode 136只出现一次的数字
leetcode·哈希算法·散列表
黎相思9 小时前
LRU Cache
c++·缓存
UIU1149 小时前
P1003 [NOIP 2011 提高组] 铺地毯
数据结构·c++·算法