- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/maximize-distance-to-closest-person/description/
Ina 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、 seatscontainsonly0sor1s,atleastone0,andatleastone1.;
给出了一个列表表示一排座位,1代表这个位置有人坐,0代表这个位置没人做。现在需要找一个位置坐,并找出坐在哪个位置时,离旁边人的座位的距离最大,是多少。
这道题是不是很眼熟呢?我翻了一下笔记,果然前几天做过啊!现在脑子里还有点印象:需要左右遍历两次。这个题目是【LeetCode】821. Shortest Distance to a Characteropen in new window。当时这个题目的要求是:给定字符串S和属于该字符串的一个字符C,要求出字符串中的每个字符到最近的C的距离。
不要看到一个是求最距离一个是求最远距离就觉得这两个题有不同。其实本题就是求最近距离的最大值!
上面的字符串题是求每个位置到C的最近距离,其实就是这个题的每个位置到1的最近距离。上面的题是要返回一个列表,这个题是要返回列表的最大值。所以就这。
我把字符串的题的解法改一下:
两步走的方案:
第一步,先假设在很远的位置有个座位有人坐,那么从左到右开始遍历,找出每个座位到其最近的左边的有人坐的位置的距离; 第二步,再假设在很远的位置有个座位有人坐,那么从右到左开始遍历,找出每个字符到其最近的右边的有人坐的位置的距离,并和第一步求出的距离进行比较,找出最小值为结果;
最后,找出这个列表的最大值。
两个技巧:
1、 设了一个比字符串长度更远的一个字符C,保证后面求最小值更新距离时一定会被更新;
2、 无论如何都用到了abs求绝对值,哪怕可能是不需要的,目的是不用费脑子思考谁大谁小了;
代码如下:
class Solution(object):
def maxDistToClosest(self, seats):
"""
:type seats: List[int]
:rtype: int
"""
index = -200000
_len = len(seats)
ans = [0] * _len
for i in range(_len):
if seats[i] == 1:
index = i
else:
ans[i] = abs(i - index)
index = -200000
for i in range(_len - 1, -1, -1):
if seats[i] == 1:
index = i
else:
ans[i] = min(abs(i - index), ans[i])
return max(ans)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
假设我有一个包含两列的表,如下所示 Size | Time | ------------------ 215 | 24868 | ------------------ 105
我不知道需要更改什么才能消除此错误: Gradient has outdated direction syntax. New syntax is likeclosest-side at 0 0 ins
我有一个包含多行的表。每行都有一个“显示详细信息”按钮和一个“隐藏详细信息”按钮。单击“显示详细信息”后,我希望隐藏详细信息按钮仅显示特定行。我认为 .closest() 函数可以工作,但还没有。 这
今晚在处理一个项目时,我最终为两个不同的页面使用了一个 .js 资源文件。一个页面包含 div 内的文本区域,另一页面包含 td 内的文本区域。想要与此文本区域的 sibling 及其父级的其他子级一
jQuery documentation州 For each element in the set, get the first element that matches the selector b
jquery closest 的文档说明如下: .closest( selector [, context ] ) ... context Type: Element A DOM element wi
我刚刚得到的信息是我的 jQuery 函数无法在 IE 和 Edge 上运行。在控制台中我有消息: 对象不支持“最接近”的属性或方法 这是 jQuery: $('body').on('change',
我目前正在将剑道网格 A 的数据设置为剑道网格 B。但是我遇到了无法读取剑道网格中未定义的属性“最接近”的问题 请帮助我.. 这是kendo B按钮的onclick代码 $("#add-Resour
我有一个 jquery 代码,可以在悬停图像时为 div 制作动画。它工作正常,但问题是我有多个具有相同类的图像和 div,并且动画同时发生在所有图像中。 我需要这个脚本在每个图像/div 中单独工作
$(".toggle-more-less").click(function() { $(this).closest(".article").toggleClass("show-hide
这是我的问题: 我有一个网格,其中图像为灰色,悬停时图像会着色(该图像无法设置为背景图像以获取信息)。 当鼠标悬停在图像上时,会出现一条文本,如果我将鼠标悬停在该文本上方,图像将停止着色。 为了解决这
这是一个更大的应用程序的一部分。 我想做的是在 Jquery 中使用 .closest 提交表单时获取 span1 中的文本。 span1 = $(this).closest("span").html
致所有人:抱歉,如果我不理解 StackOverflow 的协议(protocol)。我将立即努力纠正我在社区中的地位。话虽如此: 我试图根据调用它的内容来更改 jQuery 函数的上下文。在下面的代
目的是在页面加载时突出显示所选单选按钮的单元格。 我有以下代码: JS Fiddle . HTML c
我有这样的 HTML 我想通过单击 btn-go 获取 lat/lon/q 类的值。我知道我们可以使用最接近的方法来做到这一点,但我无法以某种方式实现这一目标。 我的JS $(document
有人可以解释一下为什么要替换以下内容 $('.post_title a').hover(function () { $(this).parent().parent().parent().par
我需要选择输入上方的标签并获取其文本。 有时是这样的: Label q1 有时是这样的: Label q2 我已与 prev() 并列但有时会有在
HTML Normal Cardsshow code <div class="tm card"> ...
我有以下列表: box-shadow:0px 0px rgb(200,200,200);Click 现在我想存储变量内的文本。 我编写了以下代
我有以下 html 结构: 当使用以下 JS 时,我认为它会很好,但它不起作用。没有错误或任何错误,只是行不通。 $('.help').on('click', function () { $(
我是一名优秀的程序员,十分优秀!