Leetcode: 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.
Solution 1: TreeMap, Time complexity: O(NlogN)
像这种在一个集合里面寻找有没有比某个数小的数,一般要么treeMap要么treeSet。Interval的题经常需要用treeMap, Data Stream as Disjoint Intervals 就是
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.
map.higherEntry(key) 找到的是一个entry with least key strictly greater than the given key, 所以20行要减一,因为interval.start可能跟current interval.end重合
用map.cellingEntry(key)找的就是一个entry with least key greater than or equal to the given key
map.lowerKey(key) Returns the greatest key strictly less than the given key, or null if there is no such key.
map.floorKey(key) Returns the greatest key less than or equal to the given key, or null if there is no such key.
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int[] findRightInterval(Interval[] intervals) {
if(intervals==null || intervals.length==0) return null;
int[] res = new int[intervals.length];
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
for (int i=0; i<intervals.length; i++) {
map.put(intervals[i].start, i);
}
for (int i=0; i<intervals.length; i++) {
Interval cur = intervals[i];
Map.Entry<Integer, Integer> entry = map.higherEntry(cur.end-1);
if (entry != null) {
res[i] = entry.getValue();
}
else res[i] = -1;
}
return res;
}
}
Solution 2: Sweep Line Solution(未深究), Time Complexity: O(NlogN)
https://discuss.leetcode.com/topic/65585/java-sweep-line-solution-o-nlogn
Leetcode: Find Right Interval的更多相关文章
- [LeetCode] Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- Java for LeetCode 057 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode: 57. Insert Interval(Hard)
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 【LeetCode】986. Interval List Intersections 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetco ...
- [Leetcode][JAVA] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
随机推荐
- not only ... but also
轉載自http://210.240.55.2/~t311/moe/engb6/b6grammar/b6notonly.htm not only ... but also ... 是「不僅‧‧‧也是‧‧ ...
- C++ string 类的 find 方法实例详解
1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...
- c++资源之不完全导引 (转)
c++资源之不完全导引 (转) 转:http://www.cnblogs.com/suiyingjie/archive/2008/02/24/1079411.html 本文2004年5月首发于< ...
- ECMAScript中关于如何获取this的定义
文章中一些名词的翻译存疑,没有查过正式的中文名称 前面都是具体过程的解释,懒得看可以直接看获取思路 有关this的取值请移步JavaScript笔记--this的取值 获取this的过程 Runtim ...
- mysql相似于oracle的to_char() to_date()方法
mysql日期和字符相互转换方法, date_format(date,'%Y-%m-%d') -------------->oracle中的to_char(); str_to_date(dat ...
- scroll、offset和client的区别
整体布局: <!DOCTYPE> <head> <meta http-equiv="Content-Type" content="text/ ...
- thinkphp多模板布局设置!!
首先开启模板布局要在配置文件添加: 'LAYOUT_ON'=>true, 'LAYOUT_NAME'=>'layout', 如果需要设置多个布局模板,就要先关闭上面的LAYOUT_ON,也 ...
- c# select标签绑定枚举,并以Description做Text显示
今天在做项目时遇到一个问题: 开发中有些字段是枚举类型如 Dept 企业表中可能有个字段 Property 性质 0:事业单位,1:私企,2:外企,但有时我们不会单独为性质这个字段定义一张表, 而是在 ...
- php课程---练习(联系人信息表)
做一个联系人表,实现增删改功能 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- C# Math
Math.Ceiling 向上进位取整.例如:Math.Ceiling(32.6)=33; Math.Ceiling(32.0)=32; Math.Floor 向下舍位取整.例如:Math.Floor ...