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. bootstrap datetimerange

    天用的了bootstrap日期插件感觉搜索的资料不是很多在此写下一些使用的心得: 插件开源地址:daterangepicker日期控件, 插件使用只要按照开源中的文档信息来就好先包括以下引用: < ...

  2. python之路——面向对象(基础篇)

    面向对象编程:类,对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 "类" 和 "对象" 来实现,所以,面向对象编程其实就是对 "类&quo ...

  3. scp详解

    scp 命令 ================== scp 可以在 2个 linux 主机间复制文件: 命令基本格式:        scp [可选参数] file_source file_targe ...

  4. Nginx-->基础-->理论-->nginx进程模型

    一.nginx的进程模型基础 如上图,是nginx的基本进程模型. 1.nginx的master进程与worker进程关系 nginx的master进程负责worker进程的管理,包括创建worker ...

  5. 乌邦图ubuntu配置iptables的NAT上网

    cat /etc/network/iptables.up.rules # Generated by iptables-save v1. :: *nat :PREROUTING ACCEPT [:] : ...

  6. .net类库里ListView的一个BUG

    今天在CSDN论坛里看一个帖子,说是在ListView中添加了条目后第一行内容不显示,为了还原他的问题我写了以下代码. private void LoadFiles(DirectoryInfo dir ...

  7. 最全的Resharper快捷键汇总

    编辑Ctrl + Space 代码完成 Ctrl + Shift + Space代码完成Ctrl + Alt + Space代码完成Ctrl + P 显示参数信息Alt + Insert 生成构造函数 ...

  8. X64操作系统组件Jmail无法正常服务问题

    故障现象: 近日,在VMware虚拟化部署迁移中,之前物理服务器中部署网站ASP组件Jmail服务一切正常,迁移完成后发现Jmail无法正常工作,其余组件能正常工作. 环境:Windows Serve ...

  9. easyui datagrid加载json

      服务端: string pseries = context.Request["ajaxSearch"].ToString().Trim(); var jsonMap = new ...

  10. 多个网站使用不同的SSH密钥登陆(zz)

    多个网站使用不同的SSH密钥登陆   1.创建不同的SSH密钥, -t指定加密方法,RSA或DSA:-C注释:-f指定文件名   www.2cto.com   ssh-keygen -t dsa -C ...