给定一组区间,对于每一个区间 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 寻找右区间的更多相关文章

  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.寻找右区间

    寻找右区间 给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的"右侧". 对于任何区间,你需要存储的满足条 ...

  3. Java实现 LeetCode 436 寻找右区间

    436. 寻找右区间 给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的"右侧". 对于任何区间,你需要存 ...

  4. 436. 寻找右区间--LeetCode_二分

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-right-interval 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出 ...

  5. 436. 寻找右区间--LeetCode_暴力

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-right-interval 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出 ...

  6. [LeetCode] Find Right Interval 找右区间

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

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

  8. 【LeetCode】寻找右区间

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

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

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

随机推荐

  1. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  2. C# Json反序列化 数据协定类型 无法反序列化 由于未找到必需的数据成员

    背景今天在使用:C# Json 序列化与反序列化 反序列化的时候出现了以下的错误信息. System.Runtime.Serialization.SerializationException: 数据协 ...

  3. 微信小程序 常见问题 小结

    1.微信小程序 尺寸单位 rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则 ...

  4. HDU 5302(Connect the Graph- 构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. 在VS2010下使用AppFace

    AppFace的介绍网上一大堆,此文仅为自己作个记录,方便以后查看. 一.需要的文件:1.AppFace.h  2.appface.lib 3.appface.dll 4.macosx_af.urf ...

  6. mongodb05---游标

    游标cursor: 通俗的说,游标不是查询结果,而是查询的返回资源,或者接口. 通过这个接口,你可以逐条读取.就像php中的fopen打开文件,得到一个资源一样, 通过资源,可以一行一行的读文件. v ...

  7. 迭代器-iteration

    class CoffrrIterator implements Iterator<Coffee> { int cunt = size; public boolean hasNext() { ...

  8. execute ps1 with pwsh.exe

    pwsh -file C:\Users\clu\source\repos\Edenred\LISA_5.0.0.0\LISA.ControlPanel\LISA.ControlPanel\bin\Re ...

  9. UVA - 11488 Hyper Prefix Sets(trie树)

    1.给n个只含0.1的串,求出这些串中前缀的最大和. 例1: 0000 0001 10101 010 结果:6(第1.2串共有000,3+3=6) 例2: 01010010101010101010 1 ...

  10. NSArray, NSSet, NSDictionary

    一.Foundation framework中用于收集cocoa对象(NSObject对象)的三种集合分别是: NSArray 用于对象有序集合(数组)NSSet 用于对象无序集合(集合) NSDic ...