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 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 <= seats.length <= 20000seatscontains only 0s or 1s, at least one0, and at least one1.
里面陷进比较多,面试的话还是要自己写完备的测试用例才行。
直接贪心求解。
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的更多相关文章
- [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 ...
- 849. Maximize Distance to Closest Person ——weekly contest 87
849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-clo ...
- 【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 解题报告(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 ...
- C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3754 访问. 在一排座位( seats)中,1 代表有人坐在座位 ...
- [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 ...
随机推荐
- hdu2328 Corporate Identity
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...
- HDU - 2732 Leapin' Lizards (拆点最大流)
题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...
- 三张图看遍Linux性能监控、测试、优化工具
Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg在最近的 LinuxCon NA 2014 大 ...
- jvm3---垃圾回收器算法
.1. GC算法 .1.1. 标记-清除算法(Mark-Sweep) 1.标记出所有需要回收的对象,在标记完成后统一回收所有被标记的对象 2.在标记完成后统一回收所有被标记的对象 缺点:一个是效率问题 ...
- Python爬虫学习笔记之Centos下安装配置Mongodb3.6
在Centos6.9上安装Mongodb时候,遇到"No package mongodb-org available"这个报错. 经过查询后,在Centos6.9上需要针对Mong ...
- Git与TortoiseGit使用方法
下载这两个工具 Git地址:https://git-for-windows.github.io/ TortoiseGit地址:http://tortoisegit.org/ 点击 ...
- 20145216史婧瑶《Java程序设计》第二次实验报告
实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...
- Linux内存管理的基本框架⭐⭐
Linux内核的映射机制设计成三层,在页面目录和页面表中间增设了一层“中间目录”.在代码中,页面目录称为PGD,中间目录称为PMD,而页面表称为PT.PT中的表项称为PTE,PTE是“Page Tab ...
- Linux crontab命令 定时任务 用法详解以及no crontab for root解决办法
最近系统服务器进行搬迁,又恰好需要使用定时任务运行程序,而我的程序主要使用PHP写的,然后总结了下定时任务的用法,但是在这里主要写的是关于crontab命令的用法,使用过程中遇到不少问题,例如no c ...
- 【目标检测】R-CNN系列与SPP-Net总结
目录 1. 前言 2. R-CNN 2.0 论文链接 2.1 概述 2.2 pre-training 2.3 不同阶段正负样本的IOU阈值 2.4 关于fine-tuning 2.5 对文章的一些思考 ...