Skip to content

Commit 08789fd

Browse files
author
Partho Biswas
committed
56_Merge_Intervals
1 parent 8524dc0 commit 08789fd

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ I have solved quite a number of problems from several topics. See the below tabl
131131
|17| [747. Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/)| [Python](https://tinyurl.com/wu6rdaw/747_Largest_Number_At_Least_Twice_of_Others.py)| |
132132
|18| [581. Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/solution/)| [Python](https://tinyurl.com/wu6rdaw/581_Shortest_Unsorted_Continuous_Subarray.py)| |
133133
|19| [904. Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/solution/)| [Python](https://tinyurl.com/wu6rdaw/904_Fruit_Into_Baskets.py)| [Article](https://www.educative.io/courses/grokking-the-coding-interview/Bn2KLlOR0lQ), [Video 1](https://www.youtube.com/watch?v=s_zu2dOkq80), [Video 2](https://www.youtube.com/watch?v=za2YuucS0tw)| Medium | [Sliding Window, Two Pointer](https://github.com/partho-maple/coding-interview-patterns) |
134-
|20| [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Python](https://tinyurl.com/wu6rdaw/56_Merge_Intervals.py)| [Article](https://leetcode.com/problems/merge-intervals/discuss/21227/7-lines-easy-Python)|
134+
|20| [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Python](https://tinyurl.com/wu6rdaw/56_Merge_Intervals.py)| [Article](https://leetcode.com/problems/merge-intervals/discuss/21227/7-lines-easy-Python), [Swift](https://tinyurl.com/wuja3c4/56_Merge_Intervals.swift) |
135135
|21| [334. Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)| [Python](https://tinyurl.com/wu6rdaw/334_Increasing_Triplet_Subsequence.py)| --- | Medium | --- |
136136
|22| [792. Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/)| [Python](https://tinyurl.com/wu6rdaw/792_Number_of_Matching_Subsequences.py)| **[Official](https://leetcode.com/problems/number-of-matching-subsequences/discuss/117634/Efficient-and-simple-go-through-words-in-parallel-with-explanation/)** | Medium | **Very tricky. Check again** |
137137
|23| [912. Sort an Array (Merge Sort)](https://leetcode.com/problems/sort-an-array/)| [Python](https://tinyurl.com/wu6rdaw/912_Sort_an_Array_(Merge__Sort).py)|--- | Medium | --- |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
3+
class Solution {
4+
func merge(_ intervals: [[Int]]) -> [[Int]] {
5+
guard intervals.count > 1 else {
6+
return intervals
7+
}
8+
var sortedIntervals: [[Int]] = intervals.sorted { $0[0] < $1[0] } // Sort the intervals by start time
9+
print(sortedIntervals)
10+
var mergedIntervals = [[Int]]()
11+
mergedIntervals.append(sortedIntervals.first!)
12+
print(mergedIntervals)
13+
for index in 1..<sortedIntervals.count {
14+
var previousInterval = mergedIntervals.last!
15+
var currentInterval = sortedIntervals[index]
16+
if previousInterval[1] >= currentInterval[0] {
17+
mergedIntervals.popLast()
18+
mergedIntervals.append([min(previousInterval[0], currentInterval[0]), max(previousInterval[1], currentInterval[1])])
19+
} else {
20+
mergedIntervals.append(currentInterval)
21+
}
22+
}
23+
return mergedIntervals
24+
}
25+
}

0 commit comments

Comments
 (0)