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 }
849. Maximize Distance to Closest Person ——weekly contest 87的更多相关文章
- 【Leetcode_easy】849. Maximize Distance to Closest Person
problem 849. Maximize Distance to Closest Person solution1: class Solution { public: int maxDistToCl ...
- [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 ...
- 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 ...
- [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 ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 849. Maximize Distance to Closest Person
class Solution { public: int maxDistToClosest(vector<int>& seats) { ; ; for(int i:seats) / ...
- [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 ...
- [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 ...
- C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3754 访问. 在一排座位( seats)中,1 代表有人坐在座位 ...
随机推荐
- MFC与QT区别
转载 https://www.cnblogs.com/forever5325/p/9597649.html QT使用的编译器是MinGW,即Linux下的GCC移植到windows的版本:MFC使用 ...
- C#怎么从List集合中随机取出其中一个值
1.首先在该命名空间下创建一个实体,和在Main方法下List集合,为后续做准备: /// <summary> /// 实体 /// </summary> public cla ...
- Spring的BeanFactory是什么?
什么是BeanFactory? 提到Spring,总是让人第一时间想起IOC容器,而IOC容器的顶层核心接口就是我们的BeanFactory,如果能够理解BeanFactory的体系结构想必能让我们对 ...
- node-macaddress
下载 node-macaddressnode-macaddress 检索Linux.OS X和Windows中的MAC地址. 关于MAC地址的一个常见误解是,每个主机只有一个MAC地址, 虽然一个主机 ...
- Go语言中的常见的几个坑
目录 1.for range 2.defer与闭包 3.map内存溢出 4.协程泄漏 5.http手动关闭 记录一下日常中遇到的几个坑,加深一下印象. 1.for range 这个是比较常见的问题了, ...
- HanLP的分词统计
HanLP的分词效果鄙人研究了HanLP,他的分词效果确实还可以,而且速度也比较快,10的数据是9000毫秒 @SneakyThrows@Overridepublic LinkedHashMap< ...
- Python基本语法之数据类型(总览)
Python的八种数据类型 Number,数值类型 String,字符串,主要用于描述文本 List,列表,一个包含元素的序列 Tuple,元组,和列表类似,但其是不可变的 Set,一个包含元素的集合 ...
- .NetCore 异步编程 - async/await
前言: 这段时间开始用.netcore做公司项目,发现前辈搭的框架通篇运用了异步编程方式,也就是async/await方式,作为一个刚接触的小白,自然不太明白其中原理,最重要的是,这个玩意如果不明白基 ...
- 【C语言学习笔记】C语言函数执行成功时,返回1和返回0,究竟哪个好?
基本上,没有人会将大段的C语言代码全部塞入 main() 函数,更好的做法是按照复用率高,耦合性低的原则,尽可能的将代码拆分不同的功能模块,并封装成函数.C语言代码的组合千变万化,因此函数的功能可能会 ...
- go 虎牙爬取
package main import ( "fmt" "github.com/antchfx/htmlquery" "io/ioutil" ...