当前位置:网站首页>[leetcode] day104 no overlapping interval
[leetcode] day104 no overlapping interval
2022-07-27 04:19:00 【Upside down, it's a circle】
subject
Answer key
Dynamic programming
Dynamic planning old friend ! Still don't know how to do ah hahaha
Algorithm :
First of all n The intervals are divided into left end points ( Or right endpoint ) Sort from small to large , Then the dynamic programming method is used to calculate the maximum number of intervals .
- State definition :dp[i] Indicates interval i For the last interval , The maximum number of non overlapping intervals that can be selected
- State transition equation :
dp[i] = max(dp[j])+1,j<i, The first j The first interval must be the same as the i The intervals do not overlap . - Initial conditions :dp[i]=1, The minimum number of removed intervals defaults to 1
- Return value :n-max(dp[i]), The rest is the minimum number of intervals to be removed
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
int n=intervals.length;
Arrays.sort(intervals,new Comparator<int[]>(){
public int compare(int[] intervals1,int[] intervals2){
return intervals1[0]-intervals2[0];
}
});
int[] dp=new int[n];
Arrays.fill(dp,1);
for(int i=1;i<n;i++){
// Traverse the previous interval
for(int j=0;j<i;j++){
// No overlap
if(intervals[j][1]<=intervals[i][0])
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
return n-Arrays.stream(dp).max().getAsInt();
}
}
Time complexity : O ( n 2 ) O(n^2) O(n2)
Spatial complexity : O ( n ) O(n) O(n)
What's wrong is , This problem of dynamic planning cannot pass , Will timeout … I thought I wrote something wrong , As a result, the official settlement also expired …
greedy
About why it is sorted by the right end of the interval ?
Sort by right endpoint , We must be able to find one End first Conference , Allow more time for later meetings , Here's the picture , choose a You can only ad, But if you choose b If you like, you can bcd, Remove an interval .
|_________| Section a
|___| Section b
|__| Section c
|______| Section d
Then the question is Greedy strategy Namely , Sort intervals by right endpoint , If the currently traversed interval [ l i , r i ] [l_i, r_i] [li,ri] Does not coincide with the previous interval , namely l i ≥ r i g h t l_i ≥right li≥right, Then we can greedily choose this range , And will right Updated to r i r_i ri
This inscription is easy to understand by drawing an interval diagram
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
int n=intervals.length;
// The intervals are sorted by the right endpoint
Arrays.sort(intervals,new Comparator<int[]>(){
public int compare(int[] intervals1,int[] intervals2){
return intervals1[1]-intervals2[1];
}
});
int res=1;// The remaining non overlapping intervals
int right=intervals[0][1];// Current rightmost endpoint position
for(int i=1;i<n;i++){
// The intervals do not overlap
if(intervals[i][0]>=right){
res++;
right=intervals[i][1];// Update the rightmost endpoint position
}
}
return n-res;
}
}
Time complexity : O ( n l o g n ) O(nlogn) O(nlogn)
Spatial complexity : O ( l o g n ) O(logn) O(logn)
↑ Are the complexity required for sorting
边栏推荐
- The problem that prevented the installation of umi4 for one day has been solved
- php+swoole
- 356页14万字高端商业办公综合楼弱电智能化系统2022版
- Rust:axum学习笔记(1) hello world
- influxDB 基础了解
- Is VR panoramic production a single weapon in the home decoration industry? Why is this?
- What is the principle difference between lateinit and lazy in kotlin
- 从根到叶的二进制数之和
- 452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition
- PSINS工具箱中轨迹生成工具详细解析
猜你喜欢

Principle of bean validation --07

Restful Fast Request 2022.2.2发布,支持批量导出文档

2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units

Redis面试题(2022)

452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition

【比赛参考】PyTorch常用代码段以及操作合集

Introduction to JVM principle

ArrayList与LinkedList区别

H. 265 web player easyplayer's method of opening video to the public

Lixia action | Yuanqi Digitalization: existing mode or open source innovation?
随机推荐
【LeetCode】Day104-无重叠区间
Cool Lehman VR panorama paves the way for you to start a business
Analysis of three common kinematic models of mobile chassis
Golang发送邮件库email
PSINS工具箱中轨迹生成工具详细解析
Golang JWT cross domain authentication
Nacos启动与登录
Summer meal | rich people are different from what you think (day 5) + power system power flow simulation (documents and matlab code)
How CentOS installs mysqldump
Article main content extraction software [based on NLP technology]
Towhee weekly model
CloudCompare&PCL 匹配点距离抑制
[OBS] dynamic bit rate: bit rate estimation
Use tag tag in golang structure
括号的最大嵌套深度
NFT digital collection system development: old brand literary magazines play with trendy Digital Collections
list模拟实现
微信input组件添加清除图标,点击清空不生效
Daily question 1: delete continuous nodes with a total value of zero from the linked list
js三种遍历数组的方法:map、forEach、filter