436:寻找右区间
This commit is contained in:
parent
a661873140
commit
825e7887bf
78
src/main/java/leetcode/editor/cn/FindRightInterval.java
Normal file
78
src/main/java/leetcode/editor/cn/FindRightInterval.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
//给你一个区间数组 intervals ,其中 intervals[i] = [starti, endi] ,且每个 starti 都 不同 。
|
||||||
|
//
|
||||||
|
// 区间 i 的 右侧区间 可以记作区间 j ,并满足 startj >= endi ,且 startj 最小化 。
|
||||||
|
//
|
||||||
|
// 返回一个由每个区间 i 的 右侧区间 的最小起始位置组成的数组。如果某个区间 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
|
||||||
|
// 每个间隔的起点都 不相同
|
||||||
|
//
|
||||||
|
// Related Topics 数组 二分查找 排序
|
||||||
|
// 👍 75 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
//436:寻找右区间
|
||||||
|
class FindRightInterval{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new FindRightInterval().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int[] findRightInterval(int[][] intervals) {
|
||||||
|
TreeMap<Integer, Integer> map = new TreeMap<>();
|
||||||
|
for (int i = 0; i < intervals.length; i++) {
|
||||||
|
map.put(intervals[i][0], i);
|
||||||
|
}
|
||||||
|
int[] result = new int[intervals.length];
|
||||||
|
for(int i = 0; i < intervals.length; i++){
|
||||||
|
Integer key = map.higherKey(intervals[i][1]-1);
|
||||||
|
result[i] = key == null?-1:map.get(key);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
45
src/main/java/leetcode/editor/cn/FindRightInterval.md
Normal file
45
src/main/java/leetcode/editor/cn/FindRightInterval.md
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<p>给你一个区间数组 <code>intervals</code> ,其中 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> ,且每个 <code>start<sub>i</sub></code> 都 <strong>不同</strong> 。</p>
|
||||||
|
|
||||||
|
<p>区间 <code>i</code> 的 <strong>右侧区间</strong> 可以记作区间 <code>j</code> ,并满足 <code>start<sub>j</sub></code><code> >= end<sub>i</sub></code> ,且 <code>start<sub>j</sub></code> <strong>最小化 </strong>。</p>
|
||||||
|
|
||||||
|
<p>返回一个由每个区间 <code>i</code> 的 <strong>右侧区间</strong> 的最小起始位置组成的数组。如果某个区间 <code>i</code> 不存在对应的 <strong>右侧区间</strong> ,则下标 <code>i</code> 处的值设为 <code>-1</code> 。</p>
|
||||||
|
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>intervals = [[1,2]]
|
||||||
|
<strong>输出:</strong>[-1]
|
||||||
|
<strong>解释:</strong>集合中只有一个区间,所以输出-1。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>intervals = [[3,4],[2,3],[1,2]]
|
||||||
|
<strong>输出:</strong>[-1, 0, 1]
|
||||||
|
<strong>解释:</strong>对于 [3,4] ,没有满足条件的“右侧”区间。
|
||||||
|
对于 [2,3] ,区间[3,4]具有最小的“右”起点;
|
||||||
|
对于 [1,2] ,区间[2,3]具有最小的“右”起点。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>intervals = [[1,4],[2,3],[3,4]]
|
||||||
|
<strong>输出:</strong>[-1, 2, -1]
|
||||||
|
<strong>解释:</strong>对于区间 [1,4] 和 [3,4] ,没有满足条件的“右侧”区间。
|
||||||
|
对于 [2,3] ,区间 [3,4] 有最小的“右”起点。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= intervals.length <= 2 * 10<sup>4</sup></code></li>
|
||||||
|
<li><code>intervals[i].length == 2</code></li>
|
||||||
|
<li><code>-10<sup>6</sup> <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||||
|
<li>每个间隔的起点都 <strong>不相同</strong></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li><li>排序</li></div></div>\n<div><li>👍 75</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user