436. 寻找右区间
给你一个区间数组 intervals
,其中 intervals[i] = [starti, endi]
,且每个 starti
都 不同 。
区间 i
的 右侧区间 可以记作区间 j
,并满足 startj
>= endi
,且 startj
最小化 。
返回一个由每个区间 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
相似题目
原站题解
python3 解法, 执行用时: 80 ms, 内存消耗: 19.2 MB, 提交时间: 2022-12-06 13:31:48
class Solution: def findRightInterval(self, intervals: List[List[int]]) -> List[int]: n = len(intervals) starts, ends = list(zip(*intervals)) starts = sorted(zip(starts, range(n))) ends = sorted(zip(ends, range(n))) ans, j = [-1] * n, 0 for end, id in ends: while j < n and starts[j][0] < end: j += 1 if j < n: ans[id] = starts[j][1] return ans
golang 解法, 执行用时: 40 ms, 内存消耗: 7.9 MB, 提交时间: 2022-12-06 13:31:34
func findRightInterval(intervals [][]int) []int { n := len(intervals) type pair struct{ x, i int } starts := make([]pair, n) ends := make([]pair, n) for i, p := range intervals { starts[i] = pair{p[0], i} ends[i] = pair{p[1], i} } sort.Slice(starts, func(i, j int) bool { return starts[i].x < starts[j].x }) sort.Slice(ends, func(i, j int) bool { return ends[i].x < ends[j].x }) ans := make([]int, n) j := 0 for _, p := range ends { for j < n && starts[j].x < p.x { j++ } if j < n { ans[p.i] = starts[j].i } else { ans[p.i] = -1 } } return ans }
javascript 解法, 执行用时: 116 ms, 内存消耗: 48.1 MB, 提交时间: 2022-12-06 13:31:17
/** * @param {number[][]} intervals * @return {number[]} */ var findRightInterval = function(intervals) { const n = intervals.length; const startIntervals = new Array(n).fill(0).map(() => new Array(2).fill(0)); const endIntervals = new Array(n).fill(0).map(() => new Array(2).fill(0)); for (let i = 0; i < n; i++) { startIntervals[i][0] = intervals[i][0]; startIntervals[i][1] = i; endIntervals[i][0] = intervals[i][1]; endIntervals[i][1] = i; } startIntervals.sort((o1, o2) => o1[0] - o2[0]); endIntervals.sort((o1, o2) => o1[0] - o2[0]); const ans = new Array(n).fill(0); for (let i = 0, j = 0; i < n; i++) { while (j < n && endIntervals[i][0] > startIntervals[j][0]) { j++; } if (j < n) { ans[endIntervals[i][1]] = startIntervals[j][1]; } else { ans[endIntervals[i][1]] = -1; } } return ans; };
java 解法, 执行用时: 18 ms, 内存消耗: 46.3 MB, 提交时间: 2022-12-06 13:31:04
class Solution { public int[] findRightInterval(int[][] intervals) { int n = intervals.length; int[][] startIntervals = new int[n][2]; int[][] endIntervals = new int[n][2]; for (int i = 0; i < n; i++) { startIntervals[i][0] = intervals[i][0]; startIntervals[i][1] = i; endIntervals[i][0] = intervals[i][1]; endIntervals[i][1] = i; } Arrays.sort(startIntervals, (o1, o2) -> o1[0] - o2[0]); Arrays.sort(endIntervals, (o1, o2) -> o1[0] - o2[0]); int[] ans = new int[n]; for (int i = 0, j = 0; i < n; i++) { while (j < n && endIntervals[i][0] > startIntervals[j][0]) { j++; } if (j < n) { ans[endIntervals[i][1]] = startIntervals[j][1]; } else { ans[endIntervals[i][1]] = -1; } } return ans; } }
java 解法, 执行用时: 10 ms, 内存消耗: 46.8 MB, 提交时间: 2022-12-06 13:30:50
class Solution { public int[] findRightInterval(int[][] intervals) { int n = intervals.length; int[][] startIntervals = new int[n][2]; for (int i = 0; i < n; i++) { startIntervals[i][0] = intervals[i][0]; startIntervals[i][1] = i; } Arrays.sort(startIntervals, (o1, o2) -> o1[0] - o2[0]); int[] ans = new int[n]; for (int i = 0; i < n; i++) { int left = 0; int right = n - 1; int target = -1; while (left <= right) { int mid = (left + right) / 2; if (startIntervals[mid][0] >= intervals[i][1]) { target = startIntervals[mid][1]; right = mid - 1; } else { left = mid + 1; } } ans[i] = target; } return ans; } }
javascript 解法, 执行用时: 100 ms, 内存消耗: 48.4 MB, 提交时间: 2022-12-06 13:30:35
/** * @param {number[][]} intervals * @return {number[]} */ var findRightInterval = function(intervals) { const n = intervals.length; const startIntervals = new Array(n).fill(0).map(() => new Array(2).fill(0)); for (let i = 0; i < n; i++) { startIntervals[i][0] = intervals[i][0]; startIntervals[i][1] = i; } startIntervals.sort((o1, o2) => o1[0] - o2[0]); const ans = new Array(n).fill(0); for (let i = 0; i < n; i++) { let left = 0; let right = n - 1; let target = -1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (startIntervals[mid][0] >= intervals[i][1]) { target = startIntervals[mid][1]; right = mid - 1; } else { left = mid + 1; } } ans[i] = target; } return ans; };
golang 解法, 执行用时: 36 ms, 内存消耗: 7.3 MB, 提交时间: 2022-12-06 13:30:22
func findRightInterval(intervals [][]int) []int { for i := range intervals { intervals[i] = append(intervals[i], i) } sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] }) n := len(intervals) ans := make([]int, n) for _, p := range intervals { i := sort.Search(n, func(i int) bool { return intervals[i][0] >= p[1] }) if i < n { ans[p[2]] = intervals[i][2] } else { ans[p[2]] = -1 } } return ans }
python3 解法, 执行用时: 80 ms, 内存消耗: 19.3 MB, 提交时间: 2022-12-06 13:30:05
class Solution: def findRightInterval(self, intervals: List[List[int]]) -> List[int]: for i, interval in enumerate(intervals): interval.append(i) intervals.sort() n = len(intervals) ans = [-1] * n for _, end, id in intervals: i = bisect_left(intervals, [end]) if i < n: ans[id] = intervals[i][2] return ans