436 Find Right Interval 尋找右區(qū)間
Description:
You are given an array of intervals, where intervals[i] = [start_i, end_i] and each start_i is unique.
The right interval for an interval i is an interval j such that start_j >= end_i and start_j is minimized.
Return an array of right interval indices for each interval i. If no right interval exists for interval i, then put -1 at index i.
Example:
Example 1:
Input: intervals = [[1,2]]
Output: [-1]
Explanation: There is only one interval in the collection, so it outputs -1.
Example 2:
Input: intervals = [[3,4],[2,3],[1,2]]
Output: [-1,0,1]
Explanation: There is no right interval for [3,4].
The right interval for [2,3] is [3,4] since start0 = 3 is the smallest start that is >= end1 = 3.
The right interval for [1,2] is [2,3] since start1 = 2 is the smallest start that is >= end2 = 2.
Example 3:
Input: intervals = [[1,4],[2,3],[3,4]]
Output: [-1,2,-1]
Explanation: There is no right interval for [1,4] and [3,4].
The right interval for [2,3] is [3,4] since start2 = 3 is the smallest start that is >= end1 = 3.
Constraints:
1 <= intervals.length <= 2 * 10^4
intervals[i].length == 2
-10^6 <= starti <= endi <= 10^6
The start point of each interval is unique.
題目描述:
給定一組區(qū)間,對于每一個區(qū)間 i耘沼,檢查是否存在一個區(qū)間 j真慢,它的起始點大于或等于區(qū)間 i 的終點澈段,這可以稱為 j 在 i 的“右側(cè)”。
對于任何區(qū)間英支,你需要存儲的滿足條件的區(qū)間 j 的最小索引,這意味著區(qū)間 j 有最小的起始點可以使其成為“右側(cè)”區(qū)間。如果區(qū)間 j 不存在蔽介,則將區(qū)間 i 存儲為 -1。最后,你需要輸出一個值為存儲的區(qū)間值的數(shù)組虹蓄。
注意:
你可以假設區(qū)間的終點總是大于它的起始點犀呼。
你可以假定這些區(qū)間都不具有相同的起始點。
示例 :
示例 1:
輸入: [ [1,2] ]
輸出: [-1]
解釋:集合中只有一個區(qū)間武花,所以輸出-1圆凰。
示例 2:
輸入: [ [3,4], [2,3], [1,2] ]
輸出: [-1, 0, 1]
解釋:對于[3,4],沒有滿足條件的“右側(cè)”區(qū)間体箕。
對于[2,3]专钉,區(qū)間[3,4]具有最小的“右”起點;
對于[1,2],區(qū)間[2,3]具有最小的“右”起點累铅。
示例 3:
輸入: [ [1,4], [2,3], [3,4] ]
輸出: [-1, 2, -1]
解釋:對于區(qū)間[1,4]和[3,4]跃须,沒有滿足條件的“右側(cè)”區(qū)間。
對于[2,3]娃兽,區(qū)間[3,4]有最小的“右”起點菇民。
思路:
對區(qū)間的左端點進行排序, 排序之后可以用二分查找查詢
時間復雜度O(nlgn), 空間復雜度O(n)
代碼:
C++:
class Solution
{
public:
vector<int> findRightInterval(vector<vector<int>>& intervals)
{
map<int, int> m;
vector<int> result;
for (int i = 0; i < intervals.size(); i++) m[intervals[i].front()] = i;
for (auto interval : intervals) result.emplace_back(m.lower_bound(interval.back()) == m.end() ? -1 : m.lower_bound(interval.back()) -> second);
return result;
}
};
Java:
class Solution {
public int[] findRightInterval(int[][] intervals) {
int count[][] = new int[intervals.length][2], n = intervals.length, result[] = new int[intervals.length], m = 0;
for (int i = 0; i < n; i++) {
count[i][0] = intervals[i][0];
count[i][1] = i;
}
Arrays.sort(count, new Comparator<int[]>(){ public int compare (int[] a, int[] b) { return a[0] - b[0]; } });
for (int interval[] : intervals) {
int target = interval[1], l = 0, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (count[mid][0] < target) l = mid + 1;
else r = mid;
}
result[m++] = (l == n ? -1 : count[l][1]);
}
return result;
}
}
Python:
class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
sorted_intervals, result = sorted([(interval[0], i) for i, interval in enumerate(intervals)]), []
for interval in intervals:
target, l, r = interval[1], 0, len(intervals)
while l < r:
mid = (l + r) >> 1
if sorted_intervals[mid][0] < target:
l = mid + 1
else:
r = mid
result.append(-1 if l == len(intervals) else sorted_intervals[l][1])
return result