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.

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;
}
};
Runtime: 64 ms, faster than 69.43% of C++ online submissions for Find Right Interval.

 Analysis:

std::map::lower_bound

      iterator lower_bound (const key_type& k);
const_iterator lower_bound (const key_type& k) const;
Return iterator to lower bound

Returns 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的更多相关文章

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

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

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

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

  4. 436. Find Right Interval ——本质:查找题目,因此二分!

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

  5. 436 Find Right Interval 寻找右区间

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

  6. 【leetcode】436. Find Right Interval

    题目如下: 解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点.既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间.注意 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  9. LeetCode---Binary Search

    475. Heaters 思路:每趟循环查找离房子最近的热水器,计算距离,最后取最大距离 public int findRadius(int[] houses, int[] heaters) { Ar ...

随机推荐

  1. 【caffe-windows】 caffe-master 之 卷积核可视化(利用matlab)

    此篇是利用matlab对caffemodel的卷积核进行可视化.只介绍了卷积核的可视化,不涉及特征图的可视化. 是参考此博客: http://blog.csdn.net/zb1165048017/ar ...

  2. 脱了裤子放屁之std::string

    一个天天跟c#奋斗的苦逼c++程序猿 改自己曾经代码的时候发现有例如以下几行. char szPath[MAX_PATH] = {0}; GetModuleFileNameA(NULL,szPath, ...

  3. 基于live555实现的跨平台高性能RTSPServer流媒体服务器EasyIPCamera

    本文转自EasyDarwin团队成员kim的博客:http://blog.csdn.net/jinlong0603/article/details/52366412 简介 EasyIPCamera是由 ...

  4. 海康DS NVR播放URL规则

    URL规定:rtsp://username:password@<address>:<port>/Streaming/Channels/<id>(?parm1=val ...

  5. java会不会出现内存泄露

    1 什么是java内存泄露 当java中的对象生命周期已经结束,本应该释放,但是却长时间不能被释放时,也就是说,内存被浪费了,就是内存泄露. 2 java内存泄露的根本原因 长生命周期的对象中持有短生 ...

  6. flume topology design . tier num 分层数目

    32:+:1 x:1 x<=8 https://flume.apache.org/FlumeUserGuide.html#flume-topology-design Flume topology ...

  7. Theseven relationsarein threecategories:equivalent, congruent, andsimilar.

    http://www.math.pitt.edu/~xfc/math2370/chap5.pdf

  8. ElasticSearch(十一)批量CURD bulk

    1.bulk语法 POST /_bulk { "delete": { "_index": "test_index", "_type ...

  9. node.js npm 安装spm失败,竟然是版本的问题

    SPM v.1.1.2 With SeaJS   SPM v1.1.2使用指南 1.SPM用途 SeaJS提供了模块化开发的机制,在代码开发完后,还需要做产品发布相关的一些操作. 这些可以通过SPM来 ...

  10. react-native填坑--react-navigation

    Navigator已经被React Native废弃了.也许你可以在另外的一个依赖库里react-native-deprecated-custom-components里找到.不过既然官方推荐的是re ...