当前位置:网站首页>力扣解法汇总436-寻找右区间
力扣解法汇总436-寻找右区间
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个区间数组 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
每个间隔的起点都 不相同
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-right-interval
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这题涉及到两次排序,为了方便理解,我们构建Model来存储这关系。Model记录start,end,和在intervals的位置index。 * 然后分别以start和end排序,生成两个集合startList和endList。 * 然后遍历endList,同时也从startList中取值。如果startList中的start大于endlist中的end。则代表符合,则记录其index。 * 如果找不到,则记录-1
代码:
public class Solution436 {
public int[] findRightInterval(int[][] intervals) {
Map<Integer, Model> map = new HashMap<>();
for (int i = 0; i < intervals.length; i++) {
Model model = new Model(intervals[i][0], intervals[i][1], i);
map.put(i, model);
}
List<Model> startList = new ArrayList<>(map.values());
startList.sort(new Comparator<Model>() {
@Override
public int compare(Model o1, Model o2) {
return o1.start - o2.start;
}
});
List<Model> endList = new ArrayList<>(map.values());
endList.sort(new Comparator<Model>() {
@Override
public int compare(Model o1, Model o2) {
return o1.end - o2.end;
}
});
int[] result = new int[intervals.length];
int startIndex = 0;
for (Model endModel : endList) {
Model startModel = null;
while (startIndex < startList.size()) {
Model model = startList.get(startIndex);
if (model.start >= endModel.end) {
startModel = model;
break;
}
startIndex++;
}
if (startModel == null) {
result[endModel.index] = -1;
} else {
result[endModel.index] = startModel.index;
}
}
return result;
}
static class Model {
int start = 0;
int end = 0;
int index = 0;
Model(int start, int end, int index) {
this.start = start;
this.end = end;
this.index = index;
}
}
}边栏推荐
- php安全开发 12博客系统的 系统模块信息的修改
- Ozzanmation action system based on SSE
- serialization and deserialization
- php开发 博客系统的公告模块的建立和引入
- On the night of the joint commissioning, I beat up my colleagues
- How WPS inserts a directory and the operating steps for quickly inserting a directory
- Ce soir - là, j'ai battu mon collègue...
- Pagination writing of PHP security open 10 article list module
- [adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin
- Huawei intermodal game or application review rejected: the application detected payment servicecatalog:x6
猜你喜欢

The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries

Three main factors determining advertising quality

Wechat applet - a case of comparing the size of numbers

Ozzanation - système d'action basé sur sse

自适应搜索广告有哪些优势?

Ozzanmation action system based on SSE

leetcodeSQL:612. Nearest distance on plane

Data system provider Jidao technology joins dragon lizard community

kali安装empire过程中遇到的各种报错解决方案

Operating mechanism of Google ads bidding
随机推荐
Manually tear the linked list (insert, delete, sort) and pointer operation
PyGame alien invasion
Basedexclassloader
Modification of system module information of PHP security development 12 blog system
What are the preparations for setting up Google search advertising series?
LeetCode Algorithm 997. Find the town judge
入手Ticwatch2
A mystery of the end of vagrant up
Redis实现消息队列的4种方案
通过搜索广告附加信息让广告更具相关性
如何为Excel中的单元格自动填充颜色
括号生成(回溯)
JSON conversion: entity classes and jsonobject are converted to each other, and list and jsonarray are converted to each other (fastjson version)
LeetCode Algorithm 1791. 找出星型图的中心节点
Wechat applet - a case of comparing the size of numbers
Basic use of MATLAB
leetcode:6. Zigzag transformation
How can low code platforms improve cost effectiveness?
Leetcode 55 jump game
Graphical data analysis | data analysis tool map