当前位置:网站首页>[interval problem] 435 Non overlapping interval
[interval problem] 435 Non overlapping interval
2022-07-05 05:16:00 【lee2813】
One 、 subject
Given a set of intervals , Find the minimum number of intervals to remove , Keep the remaining intervals from overlapping .
Be careful :
It can be said that the end of an interval is always greater than its starting point .
Section [1,2] and [2,3] The boundaries of each other “ Contact ”, But there is no overlap .
Example 1:
Input : [ [1,2], [2,3], [3,4], [1,3] ]
Output : 1
explain : remove [1,3] after , There's no overlap in the rest .
Example 2:
Input : [ [1,2], [1,2], [1,2] ]
Output : 2
explain : You need to remove two [1,2] So that the rest of the intervals don't overlap .
Example 3:
Input : [ [1,2], [2,3] ]
Output : 0
explain : You don't need to remove any intervals , Because they are already non overlapping .
Two 、 Answer key
This problem involves removing the minimum number of intervals , Analysis leads to , To make the last number of intervals reserved more , As long as the value at the end of each interval is small enough . therefore , The greedy strategy is to preferentially keep the disjoint intervals with small endings .
- First , Sort each interval according to the element at the end of the interval ( Ascending )
- then , Compare the end element value of the interval with the start value to judge whether the interval intersects , Until the end of the comparison interval
The determination of the intersection of intervals in this question :
Start from the end of the first interval , Compare with the starting element of the next interval , If the starting element of the next interval is less than prev1, Then it means that the next interval overlaps with it , discarded . In this way, it is compared with the starting element of the lower interval in turn , Until the starting element of the next interval is greater than prev1, Description no overlap , to update prev2 Is the end element of the interval , cycle .
3、 ... and 、 Code
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if(intervals.empty()) return 0;
int n = intervals.size();
sort(intervals.begin(),intervals.end(),[](vector<int>&a,vector<int>&b){
return a[1]<b[1];
});
int total = 0,prev = intervals[0][1];
for(int i = 1;i<n;i++){
if(intervals[i][0]<prev) total++;
else prev = intervals[i][1];
}
return total;
}
};
边栏推荐
- Unity synergy
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- 用 Jmeter 工具做个小型压力测试
- Dotween usage records ----- appendinterval, appendcallback
- Cocos create Jiugongge pictures
- xftp7与xshell7下载(官网)
- Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
- Time format conversion
- 被舆论盯上的蔚来,何时再次“起高楼”?
- 2022/7/1学习总结
猜你喜欢
UE fantasy engine, project structure
[turn to] MySQL operation practice (I): Keywords & functions
Research on the value of background repeat of background tiling
嵌入式数据库开发编程(零)
Stm32cubemx (8): RTC and RTC wake-up interrupt
小程序直播+電商,想做新零售電商就用它吧!
质量体系建设之路的分分合合
Generate filled text and pictures
Chinese notes of unit particle system particle effect
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
随机推荐
Lua determines whether the current time is the time of the day
xftp7与xshell7下载(官网)
嵌入式数据库开发编程(零)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Judge the position of the monster in the role under unity3d
Transport connection management of TCP
Es module and commonjs learning notes -- ESM and CJS used in nodejs
Establish cloth effect in 10 seconds
Download and use of font icons
Magnifying glass effect
cocos2dx_ Lua card flip
Download xftp7 and xshell7 (official website)
A complete attack chain
Optimization scheme of win10 virtual machine cluster
[trans]: spécification osgi
Personal required code
Research and forecast report on China's solution polymerized styrene butadiene rubber (SSBR) industry (2022 Edition)
Basic knowledge points of dictionary
用 Jmeter 工具做个小型压力测试
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low