【LeetCode】436. Find Right Interval 解题报告(Python)
【LeetCode】436. Find Right Interval 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/find-right-interval/description/
题目描述:
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the “right” of i.
For any interval i, you need to store the minimum interval j’s index, which means that the interval j has the minimum start point to build the “right” relationship for interval i. If the interval j doesn’t exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.
Note:
- You may assume the interval’s end point is always bigger than its start point.
- You may assume none of these intervals have the same start point.
Example 1:
Input: [ [1,2] ]
Output: [-1]
Explanation: There is only one interval in the collection, so it outputs -1.
Example 2:
Input: [ [3,4], [2,3], [1,2] ]
Output: [-1, 0, 1]
Explanation: There is no satisfied "right" interval for [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point;
For [1,2], the interval [2,3] has minimum-"right" start point.
Example 3:
Input: [ [1,4], [2,3], [3,4] ]
Output: [-1, 2, -1]
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point.
题目大意
给了一堆区间,找出每个区间右边最近的区间。不允许重合,每个区间的起始点不重复。如果不存在就返回-1.
解题方法
这个题主要是要使用二叉搜索,我发现我对这个理解的不够深入。
做法还是很容易理解的,因为可以使用一个字典保存每个区间的索引,因为每个区间的起点都是不同的,所以可以使用这个开始点当做区间的标记。
对起始点进行排序之后(为什么要排序?因为我们要使用二分查找),遍历每个区间,找出比这个区间的结尾大的第一个区间的起点值,然后根据这个起点值再找到这个区间的索引。
这也就是lowwer_found和higher_fount。我要补一补这方面的内容了。
代码如下:
# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution:
def findRightInterval(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[int]
"""
n = len(intervals)
start_map = {interval.start : i for i, interval in enumerate(intervals)}
start_list = [interval.start for interval in intervals]
res = []
start_list.sort()
for interval in intervals:
pos = self.higher_find(start_list, interval.end)
res.append(start_map[start_list[pos]] if pos != -1 else -1)
return res
def higher_find(self, array, v):
lo, hi = 0, len(array) - 1
first = -1
while lo <= hi:
mid = lo + (hi - lo) // 2
if array[mid] >= v:
hi = mid - 1
first = mid
else:
lo = mid + 1
return first
参考资料:
https://leetcode.com/problems/find-right-interval/discuss/156832/Python-O(n*log(n))-O(n)-slow-AF
日期
2018 年 9 月 13 日 ———— 越刷越受挫
【LeetCode】436. Find Right Interval 解题报告(Python)的更多相关文章
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- cpu的性能测试
#!/bin/bash #user%加上sys%是性能的评判标准 User_sys_a=`sar -u 1 3 |tail -1 |awk '{print $3"+"$5}'|bc ...
- Linux-各种姿势(less\vi等)打开各种类型的文件(txt/csv/xlsx等)出现不能打开(全乱码、部分乱码、二进制文件等)的问题
(一)linux各种中文乱码解决办法整理 远程登录服务器用vim在终端下编辑查看文件经常会遇见各种中文乱码问题. 做如下设置可基本解决vim中文乱码问题,首先查看系统对中文的支持locale -a | ...
- 37-Invert Binary Tree
Invert Binary Tree My Submissions QuestionEditorial Solution Total Accepted: 87818 Total Submissions ...
- Docker镜像相关操作
批量导入镜像 ll *.tgz|awk '{print $NF}'|sed -r 's#(.*)#docker load -i \1#' |bash 批量打tag docker images | se ...
- MapReduce08 数据清洗(ETL)和压缩
目录 数据清洗(ETL) ETL清洗案例 需求 需求分析 实现代码 编写WebLogMapper类 编写WebLogDriver类 打包到集群运行 压缩 概念 MR支持的压缩编码 压缩算法对比 压缩性 ...
- linux 软链接与查看历史指令
ln 说明 软连接也叫符号链接,类似于windows里的快捷方式,主要存放了路径. 基本语法 ln -s[原文件或目录][软连接名] 删除软链接 [root@hadoop102 ~]# rm -rf ...
- addict, address, adequate
addict Addiction is a biopsychosocial disorder characterized by repeated use of drugs, or repetitive ...
- APP调用系统相册,使用3DTouch重压,崩溃
崩溃:app调用系统相册,使用3DTouch重压,崩溃 问题描述 app调用系统相册,使用3DTouch重压,一般的app都会崩溃. 解决方法 写个分类即可 @implementation UICol ...
- request.getRequestDispatcher()和response.sendRedirect()区别
一.request.getRequestDispatcher().forward(request,response): 1.属于转发,也是服务器跳转,相当于方法调用,在执行当前文件的过程中转向执行目标 ...
- 【C/C++】习题3-1 得分/算法竞赛入门经典
[题目]一个由O和X组成的串,O的得分为目前连续出现的O的个数,X的得分为0.要求统计得分. 我一开始以为要输出表达式,结果好像不需要? [代码] #include <stdio.h> # ...