forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeIntervals.swift
35 lines (30 loc) · 942 Bytes
/
MergeIntervals.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Question Link: https://leetcode.com/problems/merge-intervals/
* Primary idea: Sort the original intervals and then append them one by one
* Time Complexity: O(nlogn), Space Complexity: O(n)
*
* Definition for an interval.
* public class Interval {
* public var start: Int
* public var end: Int
* public init(_ start: Int, _ end: Int) {
* self.start = start
* self.end = end
* }
* }
*/
class MergeIntervals {
func merge(_ intervals: [[Int]]) -> [[Int]] {
let intervals = intervals.sorted { return $0[0] < $1[0] }
var res = [intervals[0]]
for interval in intervals[1..<intervals.count] {
let lastEnd = res[res.count - 1][1]
if lastEnd < interval[0] {
res.append(interval)
} else {
res[res.count - 1][1] = max(res[res.count - 1][1], interval[1])
}
}
return res
}
}