Missing Ranges - Problem
Missing Ranges is a classic interval problem that tests your ability to identify gaps in data.
You're given a sorted unique integer array
For example, if you have
Goal: Return a list of range strings that cover all missing numbers with no overlaps or redundancy.
You're given a sorted unique integer array
nums and an inclusive range [lower, upper]. All elements in nums are guaranteed to be within this range. Your task is to find all the missing numbers and return them as the shortest possible list of ranges.For example, if you have
nums = [0, 1, 3, 50, 75] and range [0, 99], the missing numbers are 2, 4-49, 51-74, and 76-99. You need to represent single missing numbers as strings like "2" and ranges as "4->49".Goal: Return a list of range strings that cover all missing numbers with no overlaps or redundancy.
Input & Output
example_1.py โ Basic Case
$
Input:
nums = [0, 1, 3, 50, 75], lower = 0, upper = 99
โบ
Output:
["2", "4->49", "51->74", "76->99"]
๐ก Note:
Missing numbers: 2 (single), 4-49 (range), 51-74 (range), 76-99 (range). Each missing number or consecutive group is represented as a range string.
example_2.py โ Empty Array
$
Input:
nums = [], lower = 1, upper = 1
โบ
Output:
["1"]
๐ก Note:
When nums is empty, the entire range [lower, upper] is missing. Since lower equals upper, we have a single missing number.
example_3.py โ No Missing Numbers
$
Input:
nums = [-1], lower = -1, upper = -1
โบ
Output:
[]
๐ก Note:
The array contains the only number in the range [-1, -1], so there are no missing numbers to report.
Constraints
- -109 โค lower โค upper โค 109
- 0 โค nums.length โค 100
- lower โค nums[i] โค upper
- All values of nums are unique and sorted in ascending order
Visualization
Tap to expand
Understanding the Visualization
1
Check Lobby Area
Are there available rooms before the first booked room?
2
Walk Through Floors
Walk past each booked room and check for gaps to the next booking
3
Check Top Floors
Are there available rooms after the last booked room?
4
Generate Report
Format available rooms as ranges for the guest services team
Key Takeaway
๐ฏ Key Insight: By walking through the sorted bookings once and checking gaps, we efficiently find all available ranges without examining every room number.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code