给你一个区间数组 intervals
,其中 intervals[i] = [starti, endi]
,且每个 starti
都 不同 。
区间 i
的 右侧区间 可以记作区间 j
,并满足 startj
>= endi
,且 startj
最小化 。注意 i
可能等于 j
。
返回一个由每个区间 i
的 右侧区间 在 intervals
中对应下标组成的数组。如果某个区间 i
不存在对应的 右侧区间 ,则下标 i
处的值设为 -1
。
示例 1:
输入:intervals = [[1,2]] 输出:[-1] 解释:集合中只有一个区间,所以输出-1。
示例 2:
输入:intervals = [[3,4],[2,3],[1,2]] 输出:[-1,0,1] 解释:对于 [3,4] ,没有满足条件的“右侧”区间。 对于 [2,3] ,区间[3,4]具有最小的“右”起点; 对于 [1,2] ,区间[2,3]具有最小的“右”起点。
示例 3:
输入:intervals = [[1,4],[2,3],[3,4]] 输出:[-1,2,-1] 解释:对于区间 [1,4] 和 [3,4] ,没有满足条件的“右侧”区间。 对于 [2,3] ,区间 [3,4] 有最小的“右”起点。
提示:
1 <= intervals.length <= 2 * 104
intervals[i].length == 2
-106 <= starti <= endi <= 106
- 每个间隔的起点都 不相同
class Solution:
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
for i, v in enumerate(intervals):
v.append(i)
intervals.sort()
n = len(intervals)
ans = [-1] * n
for _, e, i in intervals:
j = bisect_left(intervals, [e])
if j < n:
ans[i] = intervals[j][2]
return ans
class Solution {
public int[] findRightInterval(int[][] intervals) {
int n = intervals.length;
List<int[]> starts = new ArrayList<>();
for (int i = 0; i < n; ++i) {
starts.add(new int[] {intervals[i][0], i});
}
starts.sort(Comparator.comparingInt(a -> a[0]));
int[] res = new int[n];
int i = 0;
for (int[] interval : intervals) {
int left = 0, right = n - 1;
int end = interval[1];
while (left < right) {
int mid = (left + right) >> 1;
if (starts.get(mid)[0] >= end) {
right = mid;
} else {
left = mid + 1;
}
}
res[i++] = starts.get(left)[0] < end ? -1 : starts.get(left)[1];
}
return res;
}
}
class Solution {
public:
vector<int> findRightInterval(vector<vector<int>>& intervals) {
int n = intervals.size();
vector<pair<int, int>> starts;
for (int i = 0; i < n; ++i) {
starts.emplace_back(make_pair(intervals[i][0], i));
}
sort(starts.begin(), starts.end());
vector<int> res;
for (auto interval : intervals) {
int left = 0, right = n - 1;
int end = interval[1];
while (left < right) {
int mid = left + right >> 1;
if (starts[mid].first >= end)
right = mid;
else
left = mid + 1;
}
res.push_back(starts[left].first < end ? -1 : starts[left].second);
}
return res;
}
};
func findRightInterval(intervals [][]int) []int {
n := len(intervals)
starts := make([][]int, n)
for i := 0; i < n; i++ {
starts[i] = make([]int, 2)
starts[i][0] = intervals[i][0]
starts[i][1] = i
}
sort.Slice(starts, func(i, j int) bool {
return starts[i][0] < starts[j][0]
})
var res []int
for _, interval := range intervals {
left, right, end := 0, n-1, interval[1]
for left < right {
mid := (left + right) >> 1
if starts[mid][0] >= end {
right = mid
} else {
left = mid + 1
}
}
val := -1
if starts[left][0] >= end {
val = starts[left][1]
}
res = append(res, val)
}
return res
}
function findRightInterval(intervals: number[][]): number[] {
const n = intervals.length;
const starts = Array.from({ length: n }).map(() => new Array<number>(2));
for (let i = 0; i < n; i++) {
starts[i][0] = intervals[i][0];
starts[i][1] = i;
}
starts.sort((a, b) => a[0] - b[0]);
return intervals.map(([_, target]) => {
let left = 0;
let right = n;
while (left < right) {
const mid = (left + right) >>> 1;
if (starts[mid][0] < target) {
left = mid + 1;
} else {
right = mid;
}
}
if (left >= n) {
return -1;
}
return starts[left][1];
});
}