[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 ...
随机推荐
- asp.net LINQ实现数据分页
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- thinkphp 数据访问
<?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...
- 关于SharpZipLib压缩分散的文件及整理文件夹的方法
今天为了解决压缩分散的文件时,发现想通过压缩对象直接进行文件夹整理很麻烦,因为SharpZipLib没有提供压缩进某个指定文件夹的功能,在反复分析了SharpZipLib提供的各个接口方法后,终于找到 ...
- matlab 连续读取多个文件
方法1: 把文件的文件名按一定的规律命名,假如:filename1.txt,filename2.txt,...,fielname100.txt,在读取的时候则可以使用循环: for i = 1:100 ...
- 大话 JSON 之 JSONObject.getString(“”) 方法 和 JSONObject.optString(“”) 的区别
运行以下代码: public static void main(String[] args) { JSONObject test = new JSONObject(); test.put(" ...
- java多线程详解(3)-线程的互斥与同步
前言:前一篇文章主要描述了多线程中访成员变量与局部变量问题,我们知道访成员变量有线程安全问题,在多线程程序中 我们可以通过使用synchronized关键字完成线程的同步,能够解决部分线程安全问题 在 ...
- eclipse无法创建Server
报错:Cannot create a server using the selected type1.退出eclipse 2.到[工程目录下]/.metadata/.plugins/org.eclip ...
- PHP单引号和双引号对待变量的不同
如果一个变量放在单引号中,会被当作字符串来处理,如果是放在双引号中,则会被当值一个变量来处理(此时可以用 {}扩起来,也可以不用). <?php $txt = "hello, this ...
- POJ 3281 Dining 最大流
饮料->牛->食物. 牛拆成两点. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include ...
- NashZhou的自我介绍
行业: 电子商务服务业,目前主要是淘宝开放平台,ISV 关键词: 电商,淘宝直通车,关键词广告,自动优化 当前目标: 广告算法 广告主自动优化 希望能在这里结识有共同爱好踏实上进的园友,共同学习,共同 ...