Leetcode算法学习日志-436 Find Right Interval

来源:互联网 发布:抛硬币实验数据 编辑:程序博客网 时间:2024/06/14 01:58

Leetcode 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:

  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.

题意分析

给定一组intervals,它们都有起始时间和结束时间,对于每一个interval,返回比这个interval的end高的最小start对应的interval的下标,如果没有,则返回-1,将所有的返回值存在一个向量中。其中每个start都互不相同。

解法分析

本题采用贪心思想,先对所有interval的start进行从小到大的排序,并利用map关联start和他们初始的下标。检查每个interval在排序后的end刚好大于哪个interval的start,返回拥有这个start的下标。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:    static bool compareStart(const Interval &a,const Interval &b){        return (a.start<b.start);    }    vector<int> findRightInterval(vector<Interval>& intervals) {        vector<int> wrong;        if(intervals.size()==0)            return wrong;        vector<int> res(intervals.size(),-1);        unordered_map<int,int> subs;        int i,j;        for(i=0;i<intervals.size();i++)            subs[intervals[i].start]=i;        sort(intervals.begin(),intervals.end(),compareStart);        for(i=0;i<intervals.size()-1;i++){            for(j=i+1;j<intervals.size();j++){                if(intervals[j].start>=intervals[i].end){                    res[subs[intervals[i].start]]=subs[intervals[j].start];                    break;                }            }               }        return res;         }};
上述代码中内层for循环的复杂度为O(k),所以算法的复杂度为排序的复杂度O(nlogn)。上述操作还可以用map直接完成,利用map对key进行的排序,以及m.lower_bound(x)完成right interval的寻找。C++代码如下:

class Solution {public:    vector<int> findRightInterval(vector<Interval>& intervals) {        map<int, int> hash;        vector<int> res;        int n = intervals.size();        for (int i = 0; i < n; ++i)            hash[intervals[i].start] = i;        for (auto in : intervals) {            auto itr = hash.lower_bound(in.end);            if (itr == hash.end()) res.push_back(-1);            else res.push_back(itr->second);        }        return res;    }};
在构造hash容器时所有元素就按start由小到大排序完成,lower_bound(in.end)返回以大于等于in.end的最小start为关键字的元素的迭代器,如果没有这样的元素,返回尾迭代器。