Skip to content

Commit

Permalink
solve "163. Missing Ranges"
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Oct 13, 2024
1 parent bdb9d9d commit 26e3465
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .plan
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ complete top interview 150

Oct 13, 2024

* solve "163. Missing Ranges"
* re-solve "56. Merge Intervals"

Oct 12, 2024
Expand Down
21 changes: 21 additions & 0 deletions src/missing_ranges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def findMissingRanges(
self, nums: list[int], lower: int, upper: int
) -> list[list[int]]:
if not nums:
return [[lower, upper]]

result: list[list[int]] = []
left = lower

for num in nums:
if num == left:
left += 1
elif num > left:
result.append([left, num - 1])
left = num + 1

if left <= upper:
result.append([left, upper])

return result
16 changes: 16 additions & 0 deletions tests/test_missing_ranges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from src.missing_ranges import Solution


@pytest.mark.parametrize(
"nums,lower,upper,expected",
(
([0, 1, 3, 50, 75], 0, 99, [[2, 2], [4, 49], [51, 74], [76, 99]]),
([-1], -1, -1, []),
([], 1, 1, [[1, 1]]),
([-1], -1, 0, [[0, 0]]),
),
)
def test_solution(nums, lower, upper, expected):
assert Solution().findMissingRanges(nums, lower, upper) == expected

0 comments on commit 26e3465

Please sign in to comment.