问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)相关推荐

  1. ​LeetCode刷题实战631:设计 Excel 求和公式

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  2. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  3. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  4. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  5. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  6. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  7. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  8. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  9. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

  10. LeetCode刷题记录8——605. Can Place Flowers(easy)

    LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...

最新文章

  1. PyTorch中AdaptiveAvgPool函数总结
  2. Fatal Error: Unable to find package java.lang in classpath or bootclasspath
  3. JavaScript常见面试题和答案
  4. redis(10)--RDB持久化
  5. Linux 禁止sendmail 自启动
  6. Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问
  7. 4g内存只有1.6g可用_linux服务器内存异常,究竟在哪消耗了2.5G?
  8. (60)FPGA比较器实现(function)
  9. Android 内部存储安装apk文件实现
  10. pg注释某一段语句不执行_@Autowired的使用:推荐对构造函数进行注释
  11. Rdlc报表出现空白页解决方法
  12. 一款盲盒的交友软件叫什么(微信恋爱脱单交友盲盒小程序制作开发介绍)
  13. Google Earth Engine(GEE)——计算NDVI\EVI\RVI\DVI\SAVI归一化植被指数、比值植被指数、差值植被指数、土壤调节植被指数、增强型植被指数和绿度植被指数计算并下载
  14. Odoo12功能增强模块
  15. android 10 多开,多开分身安卓10版
  16. Python中英文词频统计
  17. 爬虫---涨跌停股票池信息----(东方财富)
  18. 牛客月赛14-养鸽场-(二分图+转化二进制01背包)
  19. 对不起,我轻视了google的公关能力
  20. 薪水被应届生倒挂了,很不爽,我应该跳槽吗?

热门文章

  1. Visual Studio——fatal error C1902: Program database manager mismatch; please check your installation
  2. 【AI视野·今日CV 计算机视觉论文速览 第179期】Tue, 25 Feb 2020
  3. 【今日CV 视觉论文速览】 13 Feb 2019
  4. CSDN的一些年度大牛(2018)
  5. Kubernetes-Pod/ReplicaSet/Deployment/Service关系(二十一)
  6. 演练 使用变量存储商品的数据 0126
  7. django-反转路径时带上参数-适用于路径中通过命名分组的形式来捕捉参数的情景
  8. linux-查看文件类型-看本质-file
  9. javascript-dom-文档对象模型
  10. X-Frame-Options 配置