Skip to content

Commit 7fa7afa

Browse files
committed
Leetcode 56
Python Solution
1 parent 0756298 commit 7fa7afa

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages!!
3333
| 36 | Medium | [Valid Sudoku](leetcode/36-Medium-Valid-Sudoku/problem.md) | [Python](leetcode/36-Medium-Valid-Sudoku/answer.py) |
3434
| 53 | Easy | [Maximum Subarray](leetcode/53-Easy-Maximum-Subarray/problem.md) | [Java](leetcode/53-Easy-Maximum-Subarray/answer.java) |
3535
| 54 | Medium | [Spiral Matrix](leetcode/54-Medium-Spiral-Matrix/problem.md) | [Python](leetcode/54-Medium-Spiral-Matrix/answer.py) |
36+
| 56 | Medium | [Merge Intervals](leetcode/56-Medium-Merge-Intervals/problem.md) | [Python](leetcode/56-Medium-Merge-Intervals/answer.py) |
3637
| 58 | Easy | [Length of Last Word](leetcode/58-Easy-Length-Of-Last-Word/problem.md) | [Python](leetcode/58-Easy-Length-Of-Last-Word/answer.py) |
3738
| 66 | Easy | [Plus One](leetcode/66-Easy-Plus-One/problem.md) | [Python](leetcode/66-Easy-Plus-One/answer.py) |
3839
| 69 | Easy | [Sqrt()](leetcode/69-Easy-Sqrt/problem.md) | [Python](leetcode/69-Easy-Sqrt/answer.py) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/python
2+
3+
#------------------------------------------------------------------------------
4+
5+
# Definition for an interval.
6+
# class Interval(object):
7+
# def __init__(self, s=0, e=0):
8+
# self.start = s
9+
# self.end = e
10+
11+
class Solution(object):
12+
def merge(self, intervals):
13+
"""
14+
:type intervals: List[Interval]
15+
:rtype: List[Interval]
16+
"""
17+
result = []
18+
intervals.sort(key=lambda x: x.start)
19+
20+
for i in intervals:
21+
# If list is empty or does not overlap
22+
if not result or result[-1].end < i.start:
23+
result.append(i)
24+
25+
# If there is overlap
26+
else:
27+
# Change the end of the interval to the higher value
28+
result[-1].end = max(result[-1].end, i.end)
29+
30+
return result
31+
32+
#------------------------------------------------------------------------------
33+
#Testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Problem 56: Merge Intervals
2+
3+
4+
#### Difficulty: Medium
5+
6+
#### Problem
7+
8+
Given a collection of intervals, merge all overlapping intervals.
9+
10+
#### Example
11+
12+
Given the matrix
13+
14+
<pre>
15+
16+
Given [1,3],[2,6],[8,10],[15,18],
17+
18+
return [1,6],[8,10],[15,18].
19+
20+
</pre>
21+
22+
Return ```[1,2,3,6,9,8,7,4,5]```

0 commit comments

Comments
 (0)