Skip to content

Commit 34b4790

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 57 Insert Interval
1 parent d13b5aa commit 34b4790

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
3131
- [49 Group Anagrams](https://leetcode.com/problems/group-anagrams/description/)
3232
- [55 Jump Game](https://leetcode.com/problems/jump-game/description/)
3333
- [56 Merge Intervals](https://leetcode.com/problems/merge-intervals/description/)
34+
- [57 Insert Interval](https://leetcode.com/problems/insert-interval/description/)
3435
- [80 Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/)
3536
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
3637
- [121 Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def insert(
6+
self,
7+
intervals: List[List[int]],
8+
newInterval: List[int],
9+
) -> List[List[int]]:
10+
"""
11+
You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi]
12+
represent the start and the end of the ith interval and intervals is sorted in ascending order by starti.
13+
You are also given an interval newInterval = [start, end] that represents the start and end of another interval.
14+
15+
Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and
16+
intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).
17+
18+
Return intervals after the insertion.
19+
20+
Note that you don't need to modify intervals in-place. You can make a new array and return it.
21+
"""
22+
output = []
23+
i = 0
24+
25+
# Add all intervals that come before newInterval
26+
while i < len(intervals) and intervals[i][1] < newInterval[0]:
27+
output.append(intervals[i])
28+
i += 1
29+
30+
# Merge overlapping intervals
31+
while i < len(intervals) and intervals[i][0] <= newInterval[1]:
32+
newInterval[0] = min(newInterval[0], intervals[i][0])
33+
newInterval[1] = max(newInterval[1], intervals[i][1])
34+
i += 1
35+
output.append(newInterval)
36+
37+
# Add remaining intervals
38+
while i < len(intervals):
39+
output.append(intervals[i])
40+
i += 1
41+
42+
return output

tests/test_57_insert_interval.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._57_insert_interval import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["intervals", "newInterval", "expected"],
10+
argvalues=[
11+
([[1, 3], [6, 9]], [2, 5], [[1, 5], [6, 9]]),
12+
(
13+
[[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]],
14+
[4, 8],
15+
[[1, 2], [3, 10], [12, 16]],
16+
),
17+
],
18+
)
19+
def test_func(
20+
intervals: List[List[int]],
21+
newInterval: List[int],
22+
expected: List[List[int]],
23+
):
24+
inserted_intervals = Solution().insert(intervals, newInterval)
25+
assert inserted_intervals == expected

0 commit comments

Comments
 (0)