当前位置:网站首页>Interval merging [key problems + Solutions]
Interval merging [key problems + Solutions]
2022-07-20 16:11:00 【REN_ Linsen】
Interval merging
Preface
Today's clock in algorithm problem , Draw to the consolidation interval , Take it as the carrier , Show my personal understanding of problem solving .
One 、 Merge range
Two 、 Explain
1、idea
target: Merge repeated intervals .
Personally, I think the ability to grasp the core issues is the pre ability :1- What is a repeating interval ?2- What will the merger look like ?
1- What is a repeating interval ? Set interval [a,b] and [c,d], When a > d || b < c Time does not repeat ; conversely a <= d && b >= c Then the two intervals repeat .
2- What will the merger look like ? There are conditions for determining the repetition interval , It needs to be merged into [min(a,c),max(b,d)]
Actual scheme 1 : Violence law : Merge the two intervals in the above way , New areas join list, Combined 2 Intervals remove fall .O(N)/O(N)
Personally think that : Routines are tools , To solve the core problem we found .
There is a routine : A routine of two-dimensional arrays like this is , Most of them need the first and second elements to order / The reverse .
Actual scheme II : Sequencing : In order to prevent finding all the intervals to merge each time , Sort the intervals , Naturally, the merged interval will not be considered , The remaining intervals only need to be merged one by one .( Make use of man-made order .)
2、code
package everyday.medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Interval merging
public class Merge {
/* target: Merge repeated intervals . Personally, I think the ability to grasp the core issues is the pre ability :1- What is a repeating interval ?2- What will the merger look like ? 1- What is a repeating interval ? Set interval [a,b] and [c,d], When a > d || b < c Time does not repeat ; conversely a <= d && b >= c Then the two intervals repeat . 2- What will the merger look like ? There are conditions for determining the repetition interval , It needs to be merged into [min(a,c),max(b,d)] Violence law : Merge the two intervals in the above way , New areas join list, Combined 2 Intervals remove fall .O(N)/O(N) Personally think that : Routines are tools , To solve the core problem we found . There is a routine : A routine of two-dimensional arrays like this is , Most of them need the first and second elements to order / The reverse . Sequencing : In order to prevent finding all the intervals to merge each time , Sort the intervals , Naturally, the merged interval will not be considered , The remaining intervals only need to be merged one by one .( Make use of man-made order .) */
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (o1, o2) -> {
if (o1[0] != o2[0]) return o1[0] - o2[0];
return o1[1] - o2[1];
});
List<int[]> ans = new ArrayList<>();
int[] cur = new int[]{
intervals[0][0], intervals[0][1]};
for (int i = 1; i < intervals.length; i++) {
int a = cur[0], b = cur[1], c = intervals[i][0], d = intervals[i][1];
if (a > d || b < c) {
ans.add(new int[]{
a, b});
cur[0] = c;
cur[1] = d;
continue;
}
cur[1] = Math.max(b, d);
}
ans.add(cur);
return ans.toArray(new int[0][]);
}
}
summary
- Focus on core issues , The solutions are all centered on solving the core problems .
- The routine of two-dimensional array , It needs to be sorted first , With order , Think of double pointers / The sliding window / Dichotomy, etc .
reference
边栏推荐
猜你喜欢
Understand chisel language thoroughly 19. Chisel combinational circuit (I) -- chisel combinational circuit and chisel conditional statement
MySQL数据库优化的这几种方式你都知道吗?
力士乐比例节流阀2WRCE80D001-1X/PG24/M-120
获取疫情资讯数据
微信小程序获取----onenet的数据并控制stm32的板载LED
Enter the first general codeless development platform - IVX
Csv 之 简单解决使用 Excel 打开 csv 出现中文乱码现象
Deep understanding of JUC concurrency (IX) deep understanding of CAS
Preparation Notes: opencv learning: color recognition
[matlab project practice] invoice recognition based on MATLAB (including GUI interface)
随机推荐
【redis入门系列】初识 redis以及redis的安装
PHP 匿名函数使用
Assignment of golang interface variables and call of methods
服务器白名单是什么意思
吃透Chisel语言.15.Chisel模块详解(二)——Chisel模块嵌套和ALU实现
What does the server white list mean
吃透Chisel语言.19.Chisel组合电路(一)——Chisel组合电路与Chisel条件语句
程序员不会SQL?骨灰级工程师:全等着被淘汰吧!这是必会技能!
2022 latest algorithm analysis and handwritten code interview analysis
Enter the first general codeless development platform - IVX
Spark efficient data analysis 03, spark SQL
IMX8M RTL8201F以太网调试
DS(LineLinkStorStruct)
dns劫持是什麼意思?常見的劫持有哪些?
网络原理(2)——网络开发
Introduction to GC mechanism
Xcelium XRUN用户手册
Don't understand MySQL database? Alibaba P8 architects will show you MySQL and Optimization in simple terms
DS(LineLinkStorStruct)
Bi skills - same month on month calculation