436. 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.
Approach #1:
/**
* 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) {
int len = intervals.size();
vector<int> ans;
map<int, int> temp;
for (int i = 0; i < len; ++i) {
temp[intervals[i].start] = i;
}
for (int i = 0; i < len; ++i) {
auto it = temp.lower_bound(intervals[i].end);
if (it != temp.end()) ans.push_back(it->second);
else ans.push_back(-1);
}
return ans;
}
};
std::map::lower_bound
iterator lower_bound (const key_type& k);
const_iterator lower_bound (const key_type& k) const;Return iterator to lower boundReturns an iterator pointing to the first element in the container whose key is not considered to go before k (i.e., either it is equivalent or goes after).
The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the first element for which key_comp(element_key,k) would return false.
If the map class is instantiated with the default comparison type (less), the function returns an iterator to the first element whose key is not less than k.
A similar member function, upper_bound, has the same behavior as lower_bound, except in the case that the mapcontains an element with a key equivalent to k: In this case, lower_bound returns an iterator pointing to that element, whereas upper_bound returns an iterator pointing to the next element.
Parameters
- k
- Key to search for.
Member type key_type is the type of the elements in the container, defined in map as an alias of its first template parameter (Key).Return value
An iterator to the the first element in the container whose key is not considered to go before k, or map::end if all keys are considered to go before k.
If the map object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.
Member types iterator and const_iterator are bidirectional iterator types pointing to elements (of type value_type).
Notice that value_type in map containers is itself also a pair type: pair<const key_type, mapped_type>.
436. Find Right Interval的更多相关文章
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [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 Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- 436. Find Right Interval ——本质:查找题目,因此二分!
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- 436 Find Right Interval 寻找右区间
给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”.对于任何区间,你需要存储的满足条件的区间 j 的最小索引,这意味着 ...
- 【leetcode】436. Find Right Interval
题目如下: 解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点.既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间.注意 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode---Binary Search
475. Heaters 思路:每趟循环查找离房子最近的热水器,计算距离,最后取最大距离 public int findRadius(int[] houses, int[] heaters) { Ar ...
随机推荐
- 程序猿的量化交易之路(27)--Cointrader之PriceData价格数据(14)
转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ PriceData:价格数据.价格数据 ...
- EasyIPCamera高性能摄像机RTSP服务器RTSPServer解决方案
EasyIPCamera EasyIPCamera是由EasyDarwin团队开发的一套非常稳定.易用.支持多种平台(包括Windows/Linux 32&64,Android,ARM his ...
- 开源G711A/PCMA、G711U/PCMU、G726、PCM转码AAC项目EasyAACEncoder
项目及源码地址:https://github.com/EasyDarwin/EasyAACEncoder EasyAACEncoder 是EasyDarwin开源流媒体服务团队整理.开发的一款音频转码 ...
- Spring mvc接受集合类型参数的方法
public String xxxxx(String xxxx, String xxxxx, @RequestParam("parameterList[]") List<St ...
- appium(10)-iOS predictate
iOS predictate It is worth looking at ’-ios uiautomation’ search strategy with Predicates. UIAutomat ...
- iOS 第三方登录之 QQ登录
一. 首先需要下载腾讯qq登录所需的库,下载地址是http://open.qq.com/ . 需要用到的有TencentOpenAPI.framework 和TencentOpenApi_IOS_Bu ...
- PAT天梯赛 L2-026. 小字辈 【BFS】
题目链接 https://www.patest.cn/contests/gplt/L2-026 思路 用一个二维vector 来保存 每个人的子女 然后用BFS 广搜下去,当目前的状态 是搜完的时候 ...
- 测试覆盖率Emma工具使用
Emma使用与分析 #什么是Emma EMMA 是一个开源.面向 Java 程序测试覆盖率收集和报告工具.它通过对编译后的 Java 字节码文件进行插装,在测试执行过程中收集覆盖率信息,并通过支持多种 ...
- 电脑设备对于IT人员,犹如武器对于士兵
本人做了多年Softwarer,写些感受. 比我们早的老一代程序员更是用自己的健康总结了一些经验. 先说关于健康方面: 程序员要长期坐着,这对健康损害很大,颈椎腰椎,心肺能力都会衰减.以前只是听说,自 ...
- docker中Ubuntu安装jdk1.8
1.在宿主系统下载所需要的jdk版本的gz文件 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...