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. poj 1279 -- Art Gallery (半平面交)

    鏈接:http://poj.org/problem?id=1279 Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  2. sqlserver前面加N解释

    From : http://lzde360.blog.163.com/blog/static/6780720820111026112033917/ 加上 N 代表存入数据库时以 Unicode 格式存 ...

  3. 常用的邮箱服务器(SMTP、POP3)地址、端口

    常用的邮箱服务器(SMTP.POP3)地址.端口 参考网址:http://wenku.baidu.com/link?url=IPv15rPRkd0nsuGH0Dm0A5kFyRaeHJY2_gYpDW ...

  4. mysql概要(六)连接

    内连接 [join on / from 表1,表二 ]效果一样 区别是:可以理解为首先取得笛卡儿积后,再 内连接:取俩表的匹配数据: 左连接:取的俩表匹配数据,并且保留未匹配数据中左表的数据,右表数据 ...

  5. Nexus4_文件名乱码

    1. 官方的出厂映像 for Android4.4:occam-krt16s-factory-2006f418.tgz 2. 自己编译的 Android-4.4_r1 (AOSP on Mako) 映 ...

  6. hdu 4864 Task

    题目链接:hdu 4864 其实就是个贪心,只是当初我想的有偏差,贪心的思路不对,应该是这样子的: 因为 xi 的权值更重,所以优先按照 x 来排序,而这样的排序方式决定了在满足任务(即 xi > ...

  7. goLang文件遍历

    package main import (  "fmt"  "io/ioutil"  "os"  "path/filepath&q ...

  8. RTC框架

    RPC是系统间的一种通信方式,系统间常用的通信方式还有http,webservice,rpc等,一般来讲rpc比http和webservice性能高一些,常见的RPC框架有:thrift,Finagl ...

  9. java 文件操作

    1.按行读取 File file = new File(“your path”); BufferedReader reader = null; try { //System.out.println(& ...

  10. spring+redis实现缓存

    spring + redis 实现数据的缓存 1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个j ...