Leetcode算法學習日誌-436 Find Right Interval

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爲關鍵字的元素的迭代器,如果沒有這樣的元素,返回尾迭代器。

發佈了79 篇原創文章 · 獲贊 16 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章