436. Find Right Interval ——本质:查找题目,因此二分!
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.
- # Definition for an interval.
- # class Interval(object):
- # def __init__(self, s=0, e=0):
- # self.start = s
- # self.end = e
- class Solution(object):
- def findRightInterval(self, intervals):
- """
- :type intervals: List[Interval]
- :rtype: List[int]
- Input: [ [3,4], [2,3], [1,2] ]
- Output: [-1, 0, 1]
- [1, 2], [2, 3], [3, 4]
- 2, 1, 0
- 1, 0, -1
- Input: [ [1,4], [2,3], [3,4] ]
- Output: [-1, 2, -1]
- [1, 4], [2, 3], [3, 4], [4, 5] sorted
- """
- from bisect import bisect_left
- starts = []
- pos_dict = {}
- for i,v in enumerate(intervals):
- pos_dict[v.start] = i
- starts.append(v.start)
- starts.sort()
- ans = [-1]*len(intervals)
- for i,v in enumerate(intervals):
- pos = bisect_left(starts, v.end)
- if pos>=0 and pos<len(intervals):
- ans[i] = pos_dict[starts[pos]]
- return ans
436. Find Right Interval ——本质:查找题目,因此二分!的更多相关文章
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- SDUT 3376 数据结构实验之查找四:二分查找
数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...
- SDUT-3376_数据结构实验之查找四:二分查找
数据结构实验之查找四:二分查找 Time Limit: 30 ms Memory Limit: 65536 KiB Problem Description 在一个给定的无重复元素的递增序列里,查找与给 ...
- [LeetCode] 436. Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- 【leetcode】436. Find Right Interval
题目如下: 解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点.既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间.注意 ...
- [LeetCode]436 Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- C基础 旋转数组查找题目
前言 - 引言 题目: 一类有序数组旋转查值问题. 例如: 有序数组 [ , , , , , , , , ] 旋转后为 [ , , , , , , , , ] 如何从中找出一个值索引, not fou ...
- 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- 436 Find Right Interval 寻找右区间
给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”.对于任何区间,你需要存储的满足条件的区间 j 的最小索引,这意味着 ...
随机推荐
- 5.mybatis一对一表关联查询
方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集,封装联表查询的数据(去除重复的数据) SELECT * FROM class c,teacher t WHERE c.tid = t.t ...
- strange error encountered today in ROS
I reinstalled my ubuntu system and also ROS. I tested slam_karto package when some strange error cam ...
- 3.25考试(hnoi难度)----神奇的一日游
T1怕老婆 有一天hzy9819,来到了一座大城市拥有了属于他自己的一双滑板鞋.但是他还是不满足想要拥有属于自己的一栋楼,他来到了一条宽敞的大道上,一个一个记录着这些楼的层数以方便自己选择. hzy9 ...
- SQL:with ties
摘自: http://www.cnblogs.com/huanghai223/archive/2010/10/26/1861961.html “从100万条记录中的得到成绩最高的记录”.看到这个题目, ...
- 【T-SQL系列】新的排序函数
如:ROW_NUMBER.RANK.DENSE_RANK三个分析函数都是按照col1分组内从1开始排序 ROW_NUMBER() 是没有重复值的排序(即使两天记录相等也是不重复的),可以利用它来实现分 ...
- Android Studio常见问题 -- AndroidManifest.xml 覆盖问题
问题如下 D:\source-code\AndroidStudio\MyApplication\app\src\main\AndroidManifest.xmlError:(14, 9) Attrib ...
- 原生js如何获取当前所加载网页的文件路径和名称
结合使用string对象中的substr()和lastIndexOf()方法. 当前页面路径:file:///C:/Users/Administrator/Desktop/test.html < ...
- iOS - Swift SingleClass 单例类
前言 单例对象能够被整个程序所操作.对于一个单例类,无论初始化单例对象多少次,也只能有一个单例对象存在,并且该对象是全局的,能够被整个系统访问到. 单例类的创建 1.1 单例类的创建 1 单例类的创建 ...
- JavaWeb学习总结(十)--JDBC之MySQL大数据
一.基本概念 大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据,例如图像.声音.二进制文等. 但是,在mysql ...
- golang学习之beego框架配合easyui实现增删改查及图片上传
golang学习之beego框架配合easyui实现增删改查及图片上传 demo目录: upload文件夹主要放置上传的头像文件,main是主文件,所有效果如下: 主页面: 具体代码: <!DO ...