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. 02 http协议之方法与状态码

    一:HTTP请求信息和响应信息的格式 请求: ()请求行 ()请求头信息 ()请求主体信息(可以没有) () 头信息结束后和主体信息之间要空一行 请求行又分3部分 请求方法 请求路径 所用的协议 请求 ...

  2. hdu5317 RGCDQ 统计

    // hdu5317 RGCDQ // // 题目大意: // // 给定一个闭区间[l,r],定义f(x)是x的不同的质因子的个数 // 比方: 12 = 2 * 2 * 3,是两种.所以f(x) ...

  3. HDU 5313 Bipartite Graph(二分图染色+01背包水过)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  4. 3932: [CQOI2015]任务查询系统

    3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2559  Solved: 819[Submit][Sta ...

  5. jquery根据(遍历)html()的内容/根据子元素的内容(元素文本)来选择(查询),在子元素前加入元素

    <ul> <li>First</li> <li>second</li> <li>third</li> </ul ...

  6. fatal: parameter inet_interfaces: no local interface found for ::1

    https://codinfox.github.io/dev/2015/04/08/postfix-cannot-start/ Solution is straightforward: open /e ...

  7. mybatis入门(八)

    mybatis入门---更新和删除 <!-- 删除用户 --> <delete id="deleteUser" parameterType="java. ...

  8. swift-ios开发pod的使用(1)

    MAC安裝CocoaPods   http://www.cnblogs.com/surge/p/4436360.html 请注意我的环境,这个很重要 xcode版本7.3.2   mac 版本OS X ...

  9. some base knowledge

    har类型的长度被定义为一个8位字节,这很简单. short类型的长度至少为两字节.在有些计算机上,对于有些编译程序,short类型的长度可能为4字节,或者更长. int类型是一个整数的“自然”大小, ...

  10. 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...