当前位置:网站首页>力扣练习——56 寻找右区间
力扣练习——56 寻找右区间
2022-08-10 11:00:00 【qq_43403657】
56 寻找右区间
1.问题描述
给定一组区间(包含起始点和终点),对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”。
对于任何区间,你需要存储的满足条件的区间 j 的最小索引,这意味着区间 j 有最小的起始点可以使其成为“右侧”区间。如果区间 j 不存在,则将区间 i 存储为 -1。最后,你需要输出一个值为存储的区间值的数组。
注意:
你可以假设区间的终点总是大于它的起始点。
你可以假定这些区间都不具有相同的起始点。
示例 1:
输入: [ [1,2] ]
输出: [-1]
解释:集合中只有一个区间,所以输出-1。
示例 2:
输入: [ [3,4], [2,3], [1,2] ]
输出: [-1, 0, 1]
解释:对于[3,4],没有满足条件的“右侧”区间。
对于[2,3],区间[3,4]具有最小的“右”起点;
对于[1,2],区间[2,3]具有最小的“右”起点。
示例 3:
输入: [ [1,4], [2,3], [3,4] ]
输出: [-1, 2, -1]
解释:对于区间[1,4]和[3,4],没有满足条件的“右侧”区间。
对于[2,3],区间[3,4]有最小的“右”起点。
可使用以下main函数:
int main()
{
vector<vector<int> > intervals;
int n,start,end;
cin>>n;
for(int j=0; j<n; j++)
{
vector<int> aInterval;
cin>>start>>end;
aInterval.push_back(start);
aInterval.push_back(end);
intervals.push_back(aInterval);
}
vector<int> res=Solution().findRightInterval(intervals);
for(int i=0; i<res.size(); i++)
{
if (i>0)
cout<<" ";
cout<<res[i];
}
return 0;
}
2.输入说明
首先输入区间数目n,
然后输入n行,每行包括两个整数,表示起点和终点
3.输出说明
输出结果数组的内容,每两个元素之间以一个空格分隔。
4.范例
输入
3
1 4
2 3
3 4
输出
-1 2 -1
5.代码
#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstring>
#include<algorithm>
using namespace std;
vector<int> findRightInterval(vector<vector<int> >intervals) {
/*法一:部分样例通不过,why?*/
/* //记录每个区间左边端点的位置i vector<pair<int, int> >Position; int n = intervals.size(); for (int i = 0; i < n; i++) { Position.push_back({ intervals[i][0], i }); } //根据左区间的值从小到大排序 sort(intervals.begin(), intervals.end()); vector<int>ans; //遍历,寻找比当前右区间端点大的区间,且差值最小的那个区间序列i for (int i = 0; i < n; i++) { int num = -1; int left = 0; int right = n-1 ; int cur = intervals[i][1];//当前区间右端点 while (left < right) { int mid = (left + right) / 2; if (intervals[mid][0] >=cur) right = mid ;//左区间 else left = mid+1 ;//右区间 } if (intervals[right][0]>=cur) { //cout << "left=" << left << endl; for (int t = 0; t < n; t++) { if (Position[t].first == intervals[right][0]) num = t; } //num = Position[right].second; } ans.push_back(num); } return ans; */
//法二:
//参考题解:https://leetcode.cn/problems/find-right-interval/solution/xun-zhao-you-qu-jian-by-leetcode-solutio-w2ic/
/*输入: [[3,4][2,3],[1,2]] 输出 [-1,0,1] */
vector<pair<int, int> > startIntervals;
int n = intervals.size();
for (int i = 0; i < n; i++) {
startIntervals.emplace_back(intervals[i][0], i);//提取每个区间的左端点和初始下标进行结合,成为一个键值对
}
//此时startIntervals中元素为[{3,0},{2,1},{1,2}]
sort(startIntervals.begin(), startIntervals.end());//根据每个区间的左端点从小到大排序
//元素变成[{1,2},{2,1},{3,0}]
vector<int> ans(n, -1);//初始都为-1
//lower_bound(a,b,val)返回在[a,b)区间内不小于val的值
//注意,这个函数就是使用二分查找的,区间是左闭右开
//关于lower_bound函数用法请参考https://blog.csdn.net/qq_38786209/article/details/78470260
for (int i = 0; i < n; i++) {
auto it = lower_bound(startIntervals.begin(), startIntervals.end(), make_pair(intervals[i][1], 0));//此处it代表满足要求的startIntervals数组中的某个元素对
//判断it是否存在,要用下面的判断语句【迭代器用法】
if (it != startIntervals.end()) {
//it代表startIntervals中某个键值对{}
ans[i] = it->second;//it->second表示返回该键值对的第二个元素,也就是原来的下标
}
}
return ans;
}
int main()
{
vector<vector<int> > intervals;
int n, start, end;
cin >> n;
for (int j = 0; j < n; j++)
{
vector<int> aInterval;
cin >> start >> end;
aInterval.push_back(start);
aInterval.push_back(end);
intervals.push_back(aInterval);
}
vector<int> res = findRightInterval(intervals);
for (int i = 0; i < res.size(); i++)
{
if (i > 0)
cout << " ";
cout << res[i];
}
return 0;
}
边栏推荐
- AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
- 即时零售业态下如何实现自动做账?
- mysql5.7 installation and deployment - yum installation
- [Go WebSocket] 多房间的聊天室(一)思考篇
- 使用哈工大LTP测试分词并且增加自定义字典
- 金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
- AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
- 老板加薪!看我做的WPF Loading!!!
- 负载均衡原理分析与源码解读
- rider内Mono脚本找不到引用资源
猜你喜欢

TCP/IP笔记

振弦传感器及核心VM系列振弦采集模块

OneFlow源码解析:算子指令在虚拟机中的执行

AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)

Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户

C#实战:基于ItextSharp技术标签生成小工具
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板

Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan

如何使用工程仪器设备在线监测管理系统

快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
随机推荐
Open Office XML 格式里如何描述多段具有不同字体设置的段落
动作捕捉系统用于室内组合定位技术研究
HDU 1520 Anniversary party (树型dp)
暑期总结4
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
Double.doubleToLongBits() method uses
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
mysql5.7 installation and deployment - yum installation
【勇敢饭饭,不怕刷题之链表】有序链表的合并
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
Mobile and PC compatible loading and toast message plugins
为什么Redis很快
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
什么是幂等性?四种接口幂等性方案详解!
使用JMeter进行MySQL的压力测试
blocking non-blocking poll mechanism asynchronous
企业如何判断数据治理是否成功?
推荐6个自媒体领域,轻松易上手
越折腾越好用的 3 款开源 APP
让软件飞——“X+”技术揭秘