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.

Example 1:

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.

Example 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. 1 <= seats.length <= 20000
  2. seats contains only 0s or 1s, at least one 0, and at least one 1.

里面陷进比较多,面试的话还是要自己写完备的测试用例才行。

直接贪心求解。

class Solution(object):
def maxDistToClosest(self, seats):
"""
:type seats: List[int]
:rtype: int
"""
# find max contineous 0, postion
i = 0
l = len(seats)
ans = 0
while i < l:
while i<l and seats[i] == 1:
i += 1
# i == l or seats[i] == 0
start = i
while i<l and seats[i] == 0:
i += 1
# i == l or seats[i] == 1
if start == 0 or i == l:
ans = max(ans, i-start)
else:
if (i-start) & 1: # zero cnt is odd
ans = max(ans, (i-start)/2+1)
else:
ans = max(ans, (i-start-1)/2+1)
#i += 1
return ans

如果是基于计数器的思维,则写起来也还行吧:

public int maxDistToClosest(int[] seats) {
int dist = 0;
int temp = 0;
boolean closed = false;
for(int i=0; i<seats.length; ++i){
if(seats[i] == 0)
++temp;
else{
dist = Math.max(dist, closed ? (int)Math.ceil(temp/2.0) : temp);
closed = true;
temp = 0;
}
}
dist = Math.max(dist, temp);
return dist;
}

leetcode 849. Maximize Distance to Closest Person的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 849. Maximize Distance to Closest Person ——weekly contest 87

    849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-clo ...

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

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

  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. C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)

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

  9. [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 ...

随机推荐

  1. Dictionary 初始化数据

    Dictionary<string, string> dic = new Dictionary<string, string>() {                    { ...

  2. java的接口为什么不能实例化

    java的接口为什么不能实例化呢?首先,我们需要明白实例化的含义.实例化实际意义是在jvm的堆中开辟出一块内存空间,比如Student s = new Student();此处声明Student对象s ...

  3. Pentester中的XSS详解

    本次做的是Web For Pentester靶机里面的XSS题目,一共有9道题目. 关于靶机搭建参考这篇文章:渗透测试靶机的搭建 第1题(无过滤措施) 首先在后面输入xss: http://10.21 ...

  4. css 中 transition 需要注意的问题

    cubic-bezier 是 transition-timing-function 的值的一种. 四个参数的关系式如下(t 代表时间,取值范围 [0, 1]):P0(1-t)3 + 3P1t(1-t) ...

  5. Nginx访问控制_IP访问控制(http_access_module)原理、局限性、解决方法讲解

    基于IP的访问控制,基于Nginx的http_access_module模块,是Nginx本身内置的模块,不需要安装的时候配置.也就是允许哪些IP访问,不允许哪些IP访问 server { liste ...

  6. CSS 图片廊

    CSS 图片廊 一.示例一 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  7. 20145302张薇《Java程序设计》第五周学习总结

    20145302 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 try catch JVM会先尝试执行try区块中的内容,若发生错误且与catch后面的类型相符,则执行ca ...

  8. 20145327高晨 实验一 "Java开发环境的熟悉"

    实验一 Java开发环境的熟悉(Linux + Eclipse) (Windows + IDEA) 实验内容:实现Fibonacci数列功能,并进行测试. 实验步骤: Fibonacci数列(斐波拉契 ...

  9. C teaching

    L1 Preparation 1. environment codeblocks 2.variable, const 3. data type int "%d" char &quo ...

  10. 分布式缓存DistributedCache

    本文是对MR案例:Map-Join的解读. 在hadoop中,共享全局变量或全局文件的几种方法 使用Configuration的set()方法,只适合数据内容比较小的场景 将缓存文件放在HDFS上,每 ...