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 的最小索引,这意味着 ...
随机推荐
- Get started with Gazebo in ROS
Run Gazebo These three steps will run Gazebo with a default world. Install Gazebo. Open a terminal. ...
- C#形参,实参,值传递参数,引用传递参数,输出参数,参数数组的学习
1)形参 形参顾名思义就是形式上的参数,不是实际的参数,它代替实际传入方法的值.在方法体代码中代表了值本身参与运算.形参定义于参数中,它不同于方法体内局部变量,因为是一个变量,在它的作用域内不允许存在 ...
- Nginx 的RTMP打流模块配置
config配置文件: user www www; worker_processes ; error_log logs/error.log debug; #pid logs/nginx.pid; ev ...
- sqlserver 批量删除存储过程(转)
sqlserver一次只能删除一个存储过程,如果多了,需要很长时间才能删完,所以写了一段语句,直接就把当然数据库下所有用户自定义的存储过程给drop了.不过使用都请留心,当前打开的数据库哦.下面贴代码 ...
- Java源码初学_ArrayList
一.ArrayList的构造器和构造方法 在ArrayList中定义了两个空数组,分别对应当指定默认构造方法时候,指向的数组已经给定容量,但是容量等于0的时候,指向的数组.此外在构造函数中传入Coll ...
- 正则的小效果:-------> 过滤敏感词
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ajax获取城市和相应的地区
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- css-高度自适应的问题(body高度问题)
css-高度自适应的问题 对象height:100%并不能直接产生效果,是因为跟其父对象有关. #center{ height:100%; } 上面的css样式是无效的,不会产生任何效果. 需要改写: ...
- entity refenrece 在views中的运用
在一个content type中有一个field是entity reference, 那么这个字段的设置过程中会指定一个entity type和content type和一个具体内容的选择器, 然后到 ...
- linux下动态链接库.so文件 静态链接库.a文件创建及使用
转摘网址为:http://www.cnblogs.com/fengyv/archive/2012/08/10/2631313.html Linux下文件的类型是不依赖于其后缀名的,但一般来讲: ...