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. 关于erlang解析json数据

    JSON(JavaScript Object Notation)是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读.json的数据格式是文本文档格式的一种.在erlang中可以参考mochiwe ...

  2. centos 6 7 differences 区别

    命令 centos6 centos7 ifconfig 有 有 yum install -y net-tools 服务管理 chkconfig /etc/init.d/服务 systemctl sys ...

  3. linux cat命令(转载)

    来源:http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html 1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具. 查看一 ...

  4. MVC入门——详细页

    添加Action ShowDetail using System; using System.Collections.Generic; using System.Linq; using System. ...

  5. 在与SQL Server 建立 连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器

  6. thinkphp3.2独立分组的建立

    很简单,就是把默认的Home模块复制一份,放到Admin目录下,同时把namespace改成namespace Admin\Controller即可,配置项如下:

  7. 阿里妈妈-RAP项目的实践(2)

    接口详情 (id: 32872) Mock数据 接口名称 datalist1 请求类型 get 请求Url /datas/list1 接口描述 数据列表 请求参数列表 变量名 含义 类型 备注 响应参 ...

  8. [usaco2009nov]奶牛的图片

    Farmer John希望给他的N(1<=N<=100,000)只奶牛拍照片,这样他就可以向他的朋友炫耀他的奶牛.这N只奶牛被标号为1..N.在照相的那一天,奶牛们排成了一排.其中第i个位 ...

  9. 转载——Android permission 访问权限大全

    程序执行需要读取到安全敏感项必需在androidmanifest.xml中声明相关权限请求, 完整列表如下: Android.permission.ACCESS_CHECKIN_PROPERTIES ...

  10. 很好的 DHCP协议与dhcpcd分析【转】

    本文转载自:http://blog.csdn.net/gjsisi/article/details/18052369 第一部分 DHCP工作过程 DHCP的工作过程主要分为以下六个阶段:     发现 ...