leetcode56 intervals merge
给出一个区间的集合,请合并所有重叠的区间。
Given a set of intervals, merge all overlapped intervals.
Examples:
Input:
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output:
[[1,6],[8,10],[15,18]]
Input:
intervals = [[1,4],[4,5]]
Output
[[1,5]]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-intervals
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Thought and method:
In this question,we maily would like to use the first way, which is more eaiser and efficient. The second way that I learned from others and thougt it diverting. Thus, I reproduce it and present here.
1、sort first and merge than
At first, we can sort our intervals basing on the left endpoints of it.
Then, beginning to merge it.we set a array(called temp) to store the former interval
1)if the end of temp smaller than the head of the present intervals, it means that we do not have to merge it. Thus, only add it to the result and change the temp to the present intervals.
2)if the end of temp bigger than the head of the present intervals, it means we need to merge it and only change the end of temp to the end of the present intervals.
2、Moore voting
let the beginning of the intervals marking with 1
let the end of the intervals marking with -1
for example:
input:[[1,3],[2,6]] then [[1,1],[3,-1],[2,1],[6,-1],[8,1],[10,-1]]
and sort it basing on the number in the intervals
like:[[1,1],[2,1],[3,-1],[6,-1],[8,1],[10,-1]]
traverse it
do a sum of the marks
when add 1 1 -1 -1 = 0 we merge the first 4 get [1,6]
then add 1 -1 = 0 we merge the last 2 get [8,10]
Code
1、sort first and merge than
/**
* merge intervals which are overlapped
*
* @param [][]int intervals
* @return [][]int
* @private
*/
func merge(intervals [][]int) [][]int {
//Interval sorting
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
// merge
result:=[][]int{}
temp:=[]int {}
for i := 0; i < len(intervals); i++ {
if len(result)==0 || temp[1] < intervals[i][0] {
result = append(result,intervals[i])
temp = intervals[i]
}else if temp[1] < intervals[i][1]{
temp[1] = intervals[i][1]
}
}
return result
}
2、Moore voting
func merge(intervals [][]int) [][]int {
result := [][]int{}
temp := [][]int{}
//begin of the intervals:1,end of the intervals:0
for i := 0; i < len(intervals); i++ {
temp = append(temp, []int{intervals[i][0], 1})
temp = append(temp, []int{intervals[i][1], -1})
}
//begin sort following the number in intervals
sort.Slice(temp, func(i, j int) bool {
if temp[i][0] < temp[j][0] {
return true
}else if temp[i][0] == temp[j][0] && temp[i][1] >= temp[j][1] {
return true
}else{
return false
}
})
//traverse it doing a sum and merge when sum is 0
var sum int
var start int
for j := 0; j < len(temp); j++ {
if sum == 0 {
start = temp[j][0]
}
sum = sum + temp[j][1]
if sum == 0 {
result = append(result, []int{start, temp[j][0]})
}
}
return result
}
Test:
1、
Input:
intervals = [[1,3],[2,6],[8,10],[15,18]]
2、
Input:
intervals = [[1,3],[3,5],[5,6]]
Comparing to ways, the first way is better and faster. But secong way remind me of a new way in algorithm and use it into merge intervals .
Complexity
1、
time complexity
the process of merge is linear so mainly consider sort
O(nlogn)
space complexity
consider sort O(logn)
2、time complexity
mark:n^2
sort:nlogn
merge:linear
O(n^2)
space complexity
consider:mark and sort O(n)