thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

LeetCode #163: Missing Ranges

β€’ 100 words β€’ 1 min β€’ updated

LeetCode #163: Missing Ranges:

python
class Solution:
    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:
        assert nums == sorted(nums)

        nums = [lower - 1] + nums + [upper + 1]

        ans = []

        for (a, b) in zip(nums[:-1], nums[1:]):
            assert b >= a

            if (b - a) > 1:
                ans.append([a + 1, b - 1])

        return ans

This:

python
nums = [lower - 1] + nums + [upper + 1]

…is a workaround to account for the outer ranges within the same loop.

Alternatively, we could handcraft [lower, nums[0] β€” 1] and [nums[-1] + 1, upper] as needed.