博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode#56]Merge Intervals
阅读量:4931 次
发布时间:2019-06-11

本文共 3391 字,大约阅读时间需要 11 分钟。

The problem:

Given a collection of intervals, merge all overlapping intervals.

For example,

Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

My analysis:

If you could have a clear idea about each possible conditions, when sort the interval or decide the new interval's start and end boundary, the problem is very easy!1. Inorder to find out ther overlapping intervals by only traversaling the List once, we need to firstly sort the intervals (according to the start of the interval). There could be two cases:1.1 Interval i2's start position is strictly greater than i1.1.2 Interval i2's start position is the same as that of the interval i1, but i2's end position is strictly greater than i1's. Note: the sort in interval is very very important, these two are classic ways. Skill: we need to write a comparator for it.Comparator
comp = new Comparator
() { @Override public int compare(Interval i1, Interval i2) { //if i1 should be ordered before i2, return negative value if (i1.start == i2.start) return i1.end - i2.end; return i1.start - i2.start; }};2. How to decide whether two intervals are overlapping?Since we scan the intervals list from the sorted order(compare start position), we only need to compare the pre window(interval)'s end position and end window(position)'s start position. we could classify it into two cases:Assume: Interval1 {} Interval2[]2.1 { [ } ]2.2 { [ ] }from the above analysis, we could know that we could detect overlaypping through "if (pre.end >= cur.start)", and we need to decide the new window's end boundary through following way:if (pre.end >= cur.start) { pre.end = (pre.end > cur.end ? pre.end : cur.end);}3. The invariant I used in this solution:3.1 iff current interval is overlapping with the pre interval, we includ the current interval into the pre interval, by adjusting the start and end position's value.3.2 iff not, we add the pre interval into the answer set and update the pre interval into the current one. Since we add the pre interval at the next independent interval.To use this invariant, we need: 3.1 set intervals(0) as the pre intervalInterval pre = new Interval(intervals.get(0).start, intervals.get(0).end);3.2 begin the invariant from interval(1)for (int i = 1; i < intervals.size(); i++) {...}3.3 add the last interval outside the for loop.ret.add(pre);

 

My solution:

public class Solution {    public List
merge(List
intervals) { ArrayList
ret = new ArrayList
(); if (intervals == null || intervals.size() == 0) return ret; Comparator
comp = new Comparator
() { @Override public int compare(Interval i1, Interval i2) { if (i1.start == i2.start) return i1.end - i2.end; return i1.start - i2.start; } }; Collections.sort(intervals, comp); Interval pre = new Interval(intervals.get(0).start, intervals.get(0).end); for (int i = 1; i < intervals.size(); i++) { Interval cur = intervals.get(i); if (pre.end >= cur.start) { pre.end = (pre.end > cur.end ? pre.end : cur.end); } else { //a separtate interval ret.add(pre); pre = new Interval(cur.start, cur.end); } } ret.add(pre); return ret; }}

 

转载于:https://www.cnblogs.com/airwindow/p/4225311.html

你可能感兴趣的文章
HDOJ1002 A+B Problem II
查看>>
ADB server didn't ACK(adb不能开启
查看>>
网页内容抓取
查看>>
分布式和集群的区别
查看>>
Python基础(三)
查看>>
Sql server在cmd下的使用
查看>>
【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)
查看>>
swing
查看>>
Continuous integration
查看>>
前端知识点总结
查看>>
github 在ubuntu 使用--常用命令
查看>>
hl7 V2中Message Control ID的含义及应用
查看>>
IOS 4个容易混淆的属性(textAligment contentVerticalAlignment contentHorizontalAlignment contentMode)...
查看>>
iOS 修改textholder的颜色
查看>>
【资料】wod地城掉落
查看>>
C# FTPHelper(搬运)
查看>>
C#HttpHelper类1.3正式版教程与升级报告
查看>>
【转】Android 语言切换过程分析
查看>>
jpa 多对多关系的实现注解形式
查看>>
Android开发——View绘制过程源码解析(一)
查看>>