[LeetCode]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.
解法: 把这些interval按照start从小到大排序,然后对每一个interval用其end去在排好序的队列里面做二分查找,
找到符合要求的一个interval。代码:
public int[] findRightInterval(Interval[] intervals){
Interval[] sortedIntervals = Arrays.copyOf(intervals,intervals.length);
Arrays.sort(sortedIntervals,(o1, o2) -> o1.start - o2.start);
int[] result = new int[intervals.length];
for (int i = 0; i < intervals.length; i++) {
Interval current = intervals[i];
int insertIndex = Arrays.binarySearch(sortedIntervals, current, (o1, o2) -> o1.start - o2.end);
if (insertIndex < 0){
insertIndex = -insertIndex - 1;
}
if (insertIndex == intervals.length){
result[i] = -1;
}else {
Interval match = sortedIntervals[insertIndex];
for (int j = 0; j < intervals.length; j++){
if (i != j && match.start == intervals[j].start && match.end == intervals[j].end){
// System.out.println(",old index:"+j);
result[i] = j;
}
}
}
}
return result;
}
[LeetCode]436 Find Right Interval的更多相关文章
- [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 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【leetcode】436. Find Right Interval
题目如下: 解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点.既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间.注意 ...
- 【一天一道LeetCode】#57. Insert Interval
一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...
- 436 Find Right Interval 寻找右区间
给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”.对于任何区间,你需要存储的满足条件的区间 j 的最小索引,这意味着 ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 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第一刷_Insert Interval
这道题的难度跟微软的那道面试题相当. 要在集合中插入一段新的集合,相当于求两个集合的并了.对于新增加一段集合的情况,分为以下几种: 1. 增加段跟原来的全然相交,也即他的起点和终点都在被包括在原来的段 ...
- LeetCode OJ:Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- spring 异常记录
1.异常: java.lang.IllegalArgumentException: No converter found for return value of type: class java.ut ...
- bootstrap datetimerange
天用的了bootstrap日期插件感觉搜索的资料不是很多在此写下一些使用的心得: 插件开源地址:daterangepicker日期控件, 插件使用只要按照开源中的文档信息来就好先包括以下引用: < ...
- AWS Redshift summary
https://blogs.aws.amazon.com/bigdata/post/Tx31034QG0G3ED1/Top-10-Performance-Tuning-Techniques-for-A ...
- 驱动开发学习笔记. 0.05 linux 2.6 platform device register 平台设备注册 2/2 共2篇
驱动开发读书笔记. 0.05 linux 2.6 platform device register 平台设备注册 2/2 共2篇 下面这段摘自 linux源码里面的文档 : 内核版本2.6.22Doc ...
- Command(命令)-对象行为型模式
1.意图 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可撤销的操作. 2.别名 动作(Action),事务(Transaction) 3.动机 ...
- Linux基础-常用命令
常用的压缩命令 一.tar 1.压缩:tar zcvf [压缩包名].tar.gz [待压缩的文件名 ... ] 2.解压缩:tar zxvf [压缩包名].tar.gz -C [指定的解压目录] 3 ...
- RIDE安装遇到的问题及解决方法
1.按照虫师的方法,下载的wxpython3.0 ,启动robotframework-ride,无效,因为版本不一致,所以我又根据终端提示的网址:http://sourceforge.net/proj ...
- Oracle数据库自动启动Shell脚本
为了保证Oracle在下次系统重启后,能自动启动服务,这里我们可以通过一个Shell脚步来实现这个功能.假定脚步名称为/app/oracle/oraclestart.sh,其内容如下: #!/bi ...
- c语言选择排序
简单选择排序是经常用到的一种排序算法. 原理: 1.简单选择排序一句话概括:每次选择无序数列中最小的将其放在有序数列的最后. 2.在简单选择排序中,我们用初始化的数字int a[6]={2,5,6,3 ...
- 用 python实现简单EXCEL数据统计
任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...