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 }

849. Maximize Distance to Closest Person ——weekly contest 87的更多相关文章

  1. 【Leetcode_easy】849. Maximize Distance to Closest Person

    problem 849. Maximize Distance to Closest Person solution1: class Solution { public: int maxDistToCl ...

  2. [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  3. leetcode 849. Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  4. [LeetCode] 849. Maximize Distance to Closest Person 最大化最近人的距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  5. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 849. Maximize Distance to Closest Person

    class Solution { public: int maxDistToClosest(vector<int>& seats) { ; ; for(int i:seats) / ...

  7. [LeetCode] Maximize Distance to Closest Person 离最近的人的最大距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  8. [Swift]LeetCode849. 到最近的人的最大距离 | Maximize Distance to Closest Person

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  9. C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3754 访问. 在一排座位( seats)中,1 代表有人坐在座位 ...

随机推荐

  1. 在.NET中使用DiagnosticSource

    前言 DiagnosticSource是一个非常有意思的且非常有用的API,对于这些API它们允许不同的库发送命名事件,并且它们也允许应用程序订阅这些事件并处理它们,它使我们的消费者可以在运行时动态发 ...

  2. 【题解】[APIO2010]特别行动队

    Link 题目大意:一段区间的贡献是\(ax^2+bx+c,x=\sum v\),求一个划分让总区间的价值最大.分段必须连续. \(\text{Solution:}\) 设计\(dp[i]\)表示前\ ...

  3. XML节点自动生成简单实例

    有些时候我们在拼装XML的过程中,因为各种拼接会感到非常的麻烦(定义变量模型,自动生成,使用XElement再去组装),我的脑袋感觉都大了,能不能有个稍微比较快捷自动随变量自动生成XML格式的方式,看 ...

  4. 「Redis」字符串

    原文链接:https://www.changxuan.top/?p=1109 简介 Redis 中自定义的字符串结构. 字符串是 Redis 中最常用的一种数据类型,在 Redis 中专门封装了一个字 ...

  5. Python+Appium自动化测试(8)-swipe()滑动页面

    app自动化测试过程中,经常会遇到滑动屏幕操作,appium框架的话我们可以使用webdriver提供的swipe()方法来对屏幕页面进行上滑.下滑.左滑.右滑操作. 一,swipe方法介绍 swip ...

  6. 极简 Node.js 入门 - 5.1 创建 HTTP 服务器

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  7. element中过滤器filters的使用(开发小记)

    之前在开发过程中遇到这么一个问题,一串数据需要在el-table中展示,其中含有金额字段,需要将其转换成标准数据格式,即三位一个逗号间隔. 今年刚毕业就上手项目了,第一次接触的Vue,开发经验少,也忘 ...

  8. 基于python实现二叉树的遍历

    """ 二叉树实践: 用递归构建树的遍历 # 思路分析 -- 1.使用链式存储,一个Node表示一个数的节点 -- 2.节点考虑使用两个属性变量,分别表示左连接右连接 & ...

  9. go视频提取音频

    package main import ( "bytes" "fmt" "log" "os" "os/exec ...

  10. 赋予楼宇“智慧大脑”:厦门双子塔3D可视化

    前言 今年10月7日,是国务院批准设立厦门经济特区40周年纪念日.1980年的这一天,国务院正式批复同意在厦门湖里地区划出一块2.5平方公里的土地,设立经济特区.厦门,成为中国最早设立的四个经济特区之 ...