当前位置:网站首页>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;
}
边栏推荐
- 从产品维度来看 我们为什么不能完全信任Layer2?
- VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- 使用哈工大LTP测试分词并且增加自定义字典
- 程序员追求技术夯实基础学习路线建议
- Will SQL and NoSQL eventually converge?
- AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
- 越折腾越好用的 3 款开源 APP
- Redis设计与实现
猜你喜欢
rider内Mono脚本找不到引用资源
HCIP ---- VLAN
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
C#实战:基于ItextSharp技术标签生成小工具
Nocalhost - 让云原生时代的开发更高效
Spss-多元回归案例实操
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
Get started quickly and conquer three different distributed architecture calling schemes
谷歌数据中心发生“电力事故”造成 3 人受伤
短视频软件开发——平台同质化如何破局
随机推荐
快速上手,征服三种不同分布式架构调用方案
Three-phase 380V rectified voltage
3 injured in 'electrical accident' at Google data center
自媒体爆款标题怎么写?手把手教你写热门标题
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
Chapter 22 Source Code File REST API Reference (4)
POJ 3101 Astronomy (数学)
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
Module 9 - Designing an e-commerce seckill system
【无标题】
十年架构五年生活-09 五年之约如期而至
Will SQL and NoSQL eventually converge?
8月份DB-Engines 数据库排行榜最新战况
Redis设计与实现
The impact of development mode on testing
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
Several small projects that I have open sourced over the years
Double.doubleToLongBits()方法使用
【电商运营】你真的了解社交媒体营销(SMM)吗?