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 的最小索引,这意味着 ...
随机推荐
- 请求webservice接口的某方法数据
NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.com/xxx/xxxxWS?wsdl"]; NSString *soapMs ...
- MySQL修改约束
添加主键约束: ALTER TABLE tbl_name ADD [CONSTRAINT [symbol索引名]] PRIMARY KEY [index_type] (index_col ...
- 测算Redis处理实际生产请求的QPS/TPS
测算Redis处理实际生产请求的QPS/TPS Benchmark工具 redis发布版本中自带了redis-benchmark性能测试工具; 示例: 使用50个并发连接,发出100000个请求,每个 ...
- APP前端公共测试点
- 关于C++类中的成员
突然发现,如果C++的类成员中存在共有的成员,则可以通过指针的偏移来访问私有的成员变量,当然前提是对内存对齐比较清楚.只要骗过了编译器就可以为所欲为了. #include <cstdio> ...
- Redis实践操作之—— keyspace notification(键空间通知)
一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何对过期Key进行一个回调处理? 如何使用 Redis 来实现定时任务? 二.序言: 本文所说的定时任务 ...
- tomcat 启用Gzip 压缩进行优化
打开conf/server.xml文件可以看到: <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Con ...
- [转载] 跟着实例学习zookeeper 的用法
原文: http://ifeve.com/zookeeper-curato-framework/ zookeeper 的原生客户端库过于底层, 用户为了使用 zookeeper需要编写大量的代码, 为 ...
- 初识redis——mac下搭建redis环境
一.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有 ...
- Oracle 修改一行数据内存主要变化
向Oracle 数据库发出请求,修改一行数据,在内存中主要有以下变化: 1. 服务器进程将包含该行数据的块读取到内存中 2. 写redo日志.将内存中该数据块指向undo表空间中数据块的变更向量(Ch ...