当前位置:网站首页>Pulling drills - 56 Finding the right interval
Pulling drills - 56 Finding the right interval
2022-08-10 11:33:00 【qq_43403657】
56 寻找右区间
1.问题描述
给定一组区间(Contains the starting point and end point),对于每一个区间 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.输入说明
The first input interval numbern,
然后输入n行,每行包括两个整数,表示起点和终点
3.输出说明
The contents of the output array,每两个元素之间以一个空格分隔.
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) {
/*法一:Part of the sample pass,why?*/
/* //Record each interval the left endpoint locationi vector<pair<int, int> >Position; int n = intervals.size(); for (int i = 0; i < n; i++) { Position.push_back({ intervals[i][0], i }); } //According to the interval value left smallest sort(intervals.begin(), intervals.end()); vector<int>ans; //遍历,Looking for is bigger than the current right interval endpoint range,And the difference between the smallest that interval sequencei 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);//Extracting the left endpoint of each interval and the initial subscript to combine,Become a key/value pair
}
//此时startIntervals中元素为[{3,0},{2,1},{1,2}]
sort(startIntervals.begin(), startIntervals.end());//According to each interval of the left endpoint smallest
//元素变成[{1,2},{2,1},{3,0}]
vector<int> ans(n, -1);//初始都为-1
//lower_bound(a,b,val)返回在[a,b)The interval is not less thanval的值
//注意,This function is to use binary search,区间是左闭右开
//关于lower_boundFunction use please refer tohttps://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));//此处itOn behalf of meet the requirements ofstartIntervalsAn element of the array of
//判断it是否存在,To use the statement below【迭代器用法】
if (it != startIntervals.end()) {
//it代表startIntervals中某个键值对{}
ans[i] = it->second;//it->secondSaid returns the key values of the second element,Which is the original subscript
}
}
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;
}
边栏推荐
- WeChat applet, global variables change in one place and the state in other places also changes.
- Several small projects that I have open sourced over the years
- 阻塞 非阻塞 poll机制 异步
- POJ 1026 Cipher (Permutation Groups)
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
- In August the DB list latest scores - database Engines
- Codeforces 814 C. An impassioned circulation of affection (dp)
- 1-IMU参数解析以及选择
- 三相380V整流后的电压
猜你喜欢
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
Emulate stm32 directly with proteus - the programmer can be completely discarded
Spss-多元回归案例实操
如何使用工程仪器设备在线监测管理系统
为什么Redis很快
从源码角度分析UUID的实现原理
Get started quickly and conquer three different distributed architecture calling schemes
1-IMU参数解析以及选择
Redis设计与实现
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
随机推荐
力扣练习——59 从二叉搜索树到更大和树
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
谷歌数据中心发生“电力事故”造成 3 人受伤
电脑怎么设置屏幕息屏时间(日常使用分享)
Get started quickly and conquer three different distributed architecture calling schemes
负载均衡原理分析与源码解读
振弦传感器及核心VM系列振弦采集模块
阻塞 非阻塞 poll机制 异步
力扣练习——58 验证二叉搜索树
Where can I view the version record of WeChat applet submission review history?
Codeforces 814 C. An impassioned circulation of affection (dp)
【无标题】
Introduction to Software Architecture
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
基于UiAutomator2+PageObject模式开展APP自动化测试实战
基于UiAutomator2+PageObject模式开展APP自动化测试实战
力扣练习——64 最长和谐子序列
2023版揽胜运动曝光,安全、舒适一个不落
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?