C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3754 访问。
在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的。
至少有一个空座位,且至少有一人坐在座位上。
亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。
返回他到离他最近的人的最大距离。
输入:[1,0,0,0,1,0,1]
输出:2
解释:如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。因此,他到离他最近的人的最大距离是 2 。
输入:[1,0,0,0]
输出:3
解释: 如果亚历克斯坐在最后一个座位上,他离最近的人有 3 个座位远。这是可能的最大距离,所以答案是 3 。
提示:
1 <= seats.length <= 20000
seats 中只含有 0 和 1,至少有一个 0,且至少有一个 1。
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.
There is at least one empty seat, and at least one person sitting.
Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
Return that maximum distance to closest person.
Input: [1,0,0,0,1,0,1]
Output: 2
Explanation: If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.If Alex sits in any other open seat, the closest person has distance 1.Thus, the maximum distance to the closest person is 2.
Input: [1,0,0,0]
Output: 3
Explanation: If Alex sits in the last seat, the closest person is 3 seats away.This is the maximum distance possible, so the answer is 3.
Note:
1 <= seats.length <= 20000
seats contains only 0s or 1s, at least one 0, and at least one 1.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3754 访问。
public class Program {
public static void Main(string[] args) {
int[] nums = null;
nums = new int[] { 1, 1, 0, 0, 0, 1, 0 };
var res = MaxDistToClosest(nums);
Console.WriteLine(res);
nums = new int[] { 1, 0, 0, 0 };
res = MaxDistToClosest2(nums);
Console.WriteLine(res);
Console.ReadKey();
}
private static int MaxDistToClosest(int[] seats) {
//直接解法
int left, right;
int min = int.MaxValue;
int max = int.MinValue;
for(int i = 0; i < seats.Length; i++) {
if(seats[i] == 0) {
//双指针找0,即空座位
left = i; right = i;
while(--left >= 0 && seats[left] == 0) { }
while(++right <= seats.Length - 1 && seats[right] == 0) { }
//处理好边界
if(left == -1) left = int.MaxValue;
if(right == seats.Length) right = int.MaxValue;
//分析到最近的人的最大距离
if(i - left < right - i && i - left >= 0) {
min = i - left;
} else {
min = right - i;
}
//最大距离
max = Math.Max(max, min);
}
}
//返回最大距离
return max;
}
private static int MaxDistToClosest2(int[] seats) {
//记录所有非空座位
var indexOfOne = new List<int>();
for(var i = 0; i < seats.Length; i++) {
if(seats[i] == 0) continue;
indexOfOne.Add(i);
}
//找到2个非空座位的中间座位
var max = indexOfOne[0];
for(int i = 0, j = 1; i < indexOfOne.Count && j < indexOfOne.Count; i++, j++) {
var halfLength = (indexOfOne[j] - indexOfOne[i]) / 2;
if(halfLength > max) max = halfLength;
}
//处理边界
var length = seats.Length - 1 - indexOfOne[indexOfOne.Count - 1];
if(length > max) max = length;
//返回最大距离
return max;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3754 访问。
2
3
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)的更多相关文章
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结之双指针法
Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
随机推荐
- Ethical Hacking - GAINING ACCESS(24)
CLIENT SIDE ATTACKS - Detecting Trojan manually or using a sandbox Analyzing trojans Check the prope ...
- 数字图像处理 第四章 P157 小错误
问题 我认为P157中部的卷积公式是错的,f(x)h(x-m)应当写为f(m)h(x-m) 解决方法 为了证明,我就用我蹩脚的python实现一下图4.28左列 源代码如下 import numpy ...
- 7.CSMA协议
载波监听多路访问协议CSMA CS:载波侦听/监听,每一个站在发送数据之前要检测一下总线上是否有其他计算机在发送数据. MA:多点接入,表示许多计算机以多点接入的方式连接在一根总线上 协议思想:发送帧 ...
- Python协程之Gevent模块
背景 进程是操作系统分配资源的最小单位,每个进程独享4G的内存地址空间,因此进程内数据是安全的,检查间的通信需要使用特定的方法.同理,正是因为进程是数据安全的,所以导致进程的切换是一个很麻烦效率不高的 ...
- Java之枚举类
有时候,变量的取值只在一个有限的集合内. 例如:pizza的大小只有小.中.大和超大这四种尺寸.当然,可以将这些尺寸分别编码为1.2.3.4或者S.M.L.X.但这样存在着一定的隐患.在变量中很有可能 ...
- python中if及if-else如何使用
if 结构 if 结构允许程序做出选择,并根据不同的情况执行不同的操作 基本用法 比较运算符 根据 PEP 8 标准,比较运算符两侧应该各有一个空格,比如:5 == 3. PEP8 标准 ==(相等) ...
- C++11 STL Regex正则表达式与字符串字段解析
简单的日期正则表达式 一个简单的日期解析程序,从yyyy-mm-dd格式的日期字符串中,分别获取年月日. 先设置一个简单的正则表达式,4位数字的"年",1-2位数字的"月 ...
- APP自动化 -- 获取toast元素的文本内容
一.toast元素 1.表现形式:toast元素就是下图中 “操作成功” 那个一闪而过的标签. 2.特殊点:因为一闪而过,时间太短,用UIAutomatorView截屏截不到. 二.获取方法 1.用 ...
- springcloud之Eureka注册中心
参考博客:https://www.cnblogs.com/ityouknow/p/6854805.html 背景: Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Ser ...
- 轻松应对并发问题,Newbe.Claptrap 框架中 State 和 Event 应该如何理解?
Newbe.Claptrap 框架中 State 和 Event 应该如何理解?最近整理了一下项目的术语表.今天就谈谈什么是 Event 和 State. Newbe.Claptrap 是一个用于轻松 ...