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.

解法: 把这些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的更多相关文章

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

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

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

  3. 【leetcode】436. Find Right Interval

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

  4. 【一天一道LeetCode】#57. Insert Interval

    一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...

  5. 436 Find Right Interval 寻找右区间

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

  6. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  7. 436. Find Right Interval ——本质:查找题目,因此二分!

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

  8. leetcode第一刷_Insert Interval

    这道题的难度跟微软的那道面试题相当. 要在集合中插入一段新的集合,相当于求两个集合的并了.对于新增加一段集合的情况,分为以下几种: 1. 增加段跟原来的全然相交,也即他的起点和终点都在被包括在原来的段 ...

  9. LeetCode OJ:Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. Ruby-模块和类

    首先看下他们的关系 irb(main):100:0> String.class => Class irb(main):101:0> String.class.superclass  ...

  2. mac下webpagetest搭建

    我的server和agent都是在mac上搭建的,所以会和linux下有些不同   一.安装配置Apache和PHP webpagetest需要使用PHP和Apache启动服务.mac默认安装了Apa ...

  3. @EnableAutoConfiguration

    1. spring文档 解释一: Enable auto-configuration of the Spring Application Context, attempting to guess an ...

  4. SQL基本语句以及示例

    基本语句: /*dorp colunm*/ 语法:ALTER TABLE 表名   DROP COLUMN 要删除的字段 验证财务转换的正确性,查询以下两个表是否有数据 /*表连接inner jion ...

  5. MSSQLSERVER服务无法启动的解决方案

    MSSQLSERVER服务无法启动的解决方案 有时候sqlserver无法启动了,原因是mssqlserver服务没有启动,当你手动启动时,又出现服务无法响应的可恶错误提示... 笔者“有幸”遇到了, ...

  6. C# Winform学习--- 实现石头剪刀布的游戏

    本文使用winform实现简单的石头剪刀布的游戏,主要实现,电脑随机出拳,玩家手动点击出拳:实现简易背景图片3秒切换:简易统计信息. 1.效果图 2.实现代码 新建一个windows窗体程序,用数字1 ...

  7. configparser配置文件操作

    configparser 模块用于对配置操作  官方文档地址https://docs.python.org/3/library/configparser.html 导入configparser模块 i ...

  8. CentOS 7下安装Mysql 5.7

    参见http://www.07net01.com/2016/03/1355735.html 过程中需要安装perl CentOS 7 采用了 firewalld 防火墙 service firewal ...

  9. 浅淡HTML5移动Web开发

    说实话,我们这次开发移动端的项目,整个项目组的人都是第一次,最初立项的时候为是选择native app和web app还争论了一番,最后综合考虑,我们选择了web(我们选择了h5)开发.但从这两种开发 ...

  10. jetty服务器启动方法总结【备用】

    1. 使用Java命令启动 java -jar start.jar ctrl + c 关闭 终端窗口一直存在 2. 使用Java命令启动2 java -jar start.jar & 启动成功 ...