436 Find Right Interval 寻找右区间
给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”。
对于任何区间,你需要存储的满足条件的区间 j 的最小索引,这意味着区间 j 有最小的起始点可以使其成为“右侧”区间。如果区间 j 不存在,则将区间 i 存储为 -1。最后,你需要输出一个值为存储的区间值的数组。
注意:
你可以假设区间的终点总是大于它的起始点。
你可以假定这些区间都不具有相同的起始点。
示例 1:
输入: [ [1,2] ]
输出: [-1]
解释:集合中只有一个区间,所以输出-1。
示例 2:
输入: [ [3,4], [2,3], [1,2] ]
输出: [-1, 0, 1]
解释:对于[3,4],没有满足条件的“右侧”区间。
对于[2,3],区间[3,4]具有最小的“右”起点;
对于[1,2],区间[2,3]具有最小的“右”起点。
示例 3:
输入: [ [1,4], [2,3], [3,4] ]
输出: [-1, 2, -1]
解释:对于区间[1,4]和[3,4],没有满足条件的“右侧”区间。
对于[2,3],区间[3,4]有最小的“右”起点。
详见:https://leetcode.com/problems/find-right-interval/description/
C++:
方法一:
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<int> findRightInterval(vector<Interval>& intervals) {
vector<int> res, v;
unordered_map<int, int> m;
for (int i = 0; i < intervals.size(); ++i)
{
m[intervals[i].start] = i;
v.push_back(intervals[i].start);
}
sort(v.begin(), v.end(), greater<int>());
for (auto a : intervals)
{
int i = 0;
for (; i < v.size(); ++i)
{
if (v[i] < a.end)
{
break;
}
}
res.push_back((i > 0) ? m[v[i - 1]] : -1);
}
return res;
}
};
方法二:
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<int> findRightInterval(vector<Interval>& intervals) {
vector<int> res;
map<int, int> m;
for (int i = 0; i < intervals.size(); ++i)
{
m[intervals[i].start] = i;
}
for (auto a : intervals)
{
auto it = m.lower_bound(a.end);
if (it == m.end())
{
res.push_back(-1);
}
else
{
res.push_back(it->second);
}
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/6018581.html
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.寻找右区间
寻找右区间 给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的"右侧". 对于任何区间,你需要存储的满足条 ...
- Java实现 LeetCode 436 寻找右区间
436. 寻找右区间 给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的"右侧". 对于任何区间,你需要存 ...
- 436. 寻找右区间--LeetCode_二分
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-right-interval 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出 ...
- 436. 寻找右区间--LeetCode_暴力
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/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 ...
- [Swift]LeetCode436. 寻找右区间 | Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- 【LeetCode】寻找右区间
[问题] 给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”. 对于任何区间,你需要存储的满足条件的区间 j 的最小索 ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- 关于rman duplicate 一些比較重要的知识点--系列三
FYI: http://docs.oracle.com/cd/E11882_01/backup.112/e10643/rcmsynta020.htm#RCMRF126 rman duplicate d ...
- redux-thunk
1.thunk function createThunkMiddleware(extraArgument) { return ({ dispatch, getState }) => next = ...
- Hive中行列转换
1.演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu ...
- Jenkins系列之-—01 简介&新建任务
一.Jenkins 简介 Jenkins是一个可扩展的持续集成引擎. 主要用于: l 持续.自动地构建/测试软件项目.l 监控一些定时执行的任务. Jenkins拥有的特性包括: l 易于安装-只要把 ...
- JAVA注解引发的思考
自从JDK5開始Java開始添加了对元数据(MetaData)的支持,也就是注解(Annotation),到JDK7时已经有四种基本注解,新添加了一种@SafeVarargs. @Override注解 ...
- 2014/4/18 ① button与submit的区别 ②现象 : 数据库中其他值可以取到 有的却取不到 解决 看获取时“#”有无
①<input type="button" /> 这就是一个按钮.如果你不写javascript 的话,按下去什么也不会 发生. <input type=&quo ...
- DataTables warning requested unknown parameter
This is possibly the most cryptic warning message that DataTables will show. It is a short error mes ...
- ubuntu切换中英文通用方法,ubuntu中文语言
1:点击桌面右上角的齿轮,选择“system settings”进入系统设置界面
- codeforces 688D D. Remainders Game(中国剩余定理)
题目链接: D. Remainders Game time limit per test 1 second memory limit per test 256 megabytes input stan ...
- gevent 协程 使用
Python通过yield提供了对协程的基本支持,但是不完全.而第三方的gevent为Python提供了比较完善的协程支持. gevent是第三方库,通过greenlet实现协程,其基本思想是: 当一 ...