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:

  1. You may assume the interval's end point is always bigger than its start point.
  2. 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 ——本质:查找题目,因此二分!的更多相关文章

  1. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. SDUT 3376 数据结构实验之查找四:二分查找

    数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...

  3. SDUT-3376_数据结构实验之查找四:二分查找

    数据结构实验之查找四:二分查找 Time Limit: 30 ms Memory Limit: 65536 KiB Problem Description 在一个给定的无重复元素的递增序列里,查找与给 ...

  4. [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 ...

  5. 【leetcode】436. Find Right Interval

    题目如下: 解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点.既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间.注意 ...

  6. [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 ...

  7. C基础 旋转数组查找题目

    前言 - 引言 题目: 一类有序数组旋转查值问题. 例如: 有序数组 [ , , , , , , , , ] 旋转后为 [ , , , , , , , , ] 如何从中找出一个值索引, not fou ...

  8. 436. Find Right Interval

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  9. 436 Find Right Interval 寻找右区间

    给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”.对于任何区间,你需要存储的满足条件的区间 j 的最小索引,这意味着 ...

随机推荐

  1. NoSQL数据库的分布式模型

    NoSQL数据库的分布式模型 单一服务器 在一个服务器完全能够胜任工作时就没必要考虑分布式,这样部署简单,维护也方便很多: 分片 特点 数据的各个部分存放在集群的不同服务器中: 比如按字母来划分:以a ...

  2. VB6.0

    1. 安装包来自 http://msdn.itellyou.cn/ 下载的文件为: sc_vb6_ent_cd1.iso sc_vb6_ent_cd2.iso 2.安装时,"请输入产品的 I ...

  3. [转载] google mock CheatSheet

    原文: https://code.google.com/p/googlemock/wiki/CheatSheet Defining a Mock Class Mocking a Normal Clas ...

  4. poj1319Pipe Fitters

    链接 算不上几何的水题 第一种为(int)a*(int)b: 第二种分宽高交换两种讨论. 每一个的高度除第一个为1外其它的都可以看着b/sqrt(3.0)/2; #include <iostre ...

  5. JAVA入门 第五周 1多项式

    1 多项式加法(5分) 题目内容: 一个多项式可以表达为x的各次幂与系数乘积的和,比如: 现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出. 程序要处理的 ...

  6. Vim优化

    写python代码时,希望缩进是4个空格,而不是制表符tab, 在vim中,我们只需要简单配置一下就ok了,打开~/.vimrc加上下面的几行(如果已经有了,修改一下数值就行了). set tabst ...

  7. android模拟器中文乱码

    问题:在xml文件中设置的中文能正确输出,但是在java文件中设置的中文会在模拟器上呈现乱码 解决方案:在build.gradle文件中添加一行代码 android {compileOptions.e ...

  8. flexbox弹性伸缩布局

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  9. JSP中RequestDispatcher的用法

    RequestDispatcher是一个Web资源的包装器,可以用来把当前request传递到该资源,或者把新的资源包括到当前响应中.RequestDispatcher接口中定义了两个方法:inclu ...

  10. myeclipse黑色主题怎么还原

    删除workspace-->.metadata-->.plugins-->org.eclipse.core.runtime