【LeetCode】56. Merge Intervals 解題報告
56. Merge Intervals / Medium
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Constraints:
- 1 <= intervals.length <= 10^4
- intervals[i].length == 2
- 0 <= starti <= endi <= 10^4
Solution: Sorting
思路
想要判斷兩個區間 [a,b] 與 [c,d] 是重疊,有兩個條件
- a <= c
- c-b <= 0
第一個條件可以藉由排序達成,所以我們先把 intervals 排序,這時裡面的 vector 會按照第一個元素的大小排列。
接下來只要判斷第二個條件就可以了,如果 c 比 b 大,那就不是重疊了,可以直接把目前的 vector push進最後解答的 vector。如果重疊,我們就已經確定左邊界了(因為排序過,a 一定是最小的),至於右邊界,則是 max(b,d),有可能出現 [c,d] 是 [a,b] 的子區間這種狀況。
效能
Complexity
- Time Complexity: O(NlogN)
- Space Complexity: O(N)
LeetCode Result
- Runtime: 16 ms
- Memory Usage: 14.1 MB
- https://leetcode.com/submissions/detail/535062959/
Code
1 | class Solution { |