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 代表有人坐在座位 ...
随机推荐
- 【奇淫巧技】sqlmap绕过过滤的tamper脚本分类汇总
sqlmap绕过过滤的tamper脚本分类汇总
- P2034 选择数字 / P2627 [USACO11OPEN]Mowing the Lawn G
Link 题目描述 给定一行 \(n\) 个非负整数 \(a[1]..a[n]\) .现在你可以选择其中若干个数,但不能有超过 \(k\) 个连续的数字被选择.你的任务是使得选出的数字的和最大. 输入 ...
- JVM性能调优(4) —— 性能调优工具
前序文章: JVM性能调优(1) -- JVM内存模型和类加载运行机制 JVM性能调优(2) -- 垃圾回收器和回收策略 JVM性能调优(3) -- 内存分配和垃圾回收调优 一.JDK工具 先来看看有 ...
- Vue学习 一 环境搭建
Vue ui 启动 创建一个新项目 包管理器 npm Bable 转换js 至低版本的支持(兼容低版本) TypeScript 对TypeScript 的支持 (暂时不需要) PWA ...
- python中input()函数与print()函数
一.input()函数详解 二.print()函数详解 三.类型转换
- 多测师讲解html _段落标签002_高级讲师肖sir
<html> <head> <meta charset="UTF-8"> <title>段落标签</title> < ...
- K8S节点异常怎么办?TKE"节点健康检查和自愈"来帮忙
节点健康检测 意义 在K8S集群运行的过程中,节点常常会因为运行时组件的问题.内核死锁.资源不足等各种各样的原因不可用.Kubelet默认对节点的PIDPressure.MemoryPressure. ...
- JSON,数组根据字段多次分组
我们在前端开发过程中,遇到json对象,有时会需要多次分组.比如说,先按照是业务分组,然后再按照产品线分组,然后通过table或其他方式展示或操作 var obj1=[ { "demp&qu ...
- 10年经验17张图带你进入gitflow企业项目代码版本管理的最佳实践
前言 对于项目版本管理,你是否存在这样的痛点:项目分支多而杂不好管理,git log界面commit信息错乱复杂无规范,版本回退不知道选择什么版本合适--. 项目版本管理的最佳实践系列,笔者将以两篇文 ...
- C 和 C++ 打起来了!曾今最亲密的伙伴到现今的不爽?
70年代初,贝尔实验室创建了C语言,它是开发UNIX的副产品.很快C就成为了最受欢迎的编程语言之一.但是对于Bjarne Stroustrup来说,C的表达能力还不够.于是,他在1983年的博士论文中 ...