You are given a sorted unique integer array nums.
A range [a,b] is the set of all integers from a to b (inclusive).
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
Each range [a,b] in the list should be output as:
"a->b" if a != b"a" if a == bExample 1:
Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"
Example 2:
Input: nums = [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
Constraints:
0 <= nums.length <= 20-231 <= nums[i] <= 231 - 1nums are unique.nums is sorted in ascending order.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a sorted list of numbers and you need to group consecutive numbers together into ranges. The brute force strategy involves exhaustively examining the numbers one by one to find unbroken sequences from start to finish.
Here's how the algorithm would work step-by-step:
def summary_ranges(numbers):
summary_results = []
total_numbers = len(numbers)
index_pointer = 0
# We loop until every single number is processed into a range
while index_pointer < total_numbers:
start_pointer = index_pointer
# We extend the range as long as consecutive numbers increase by one
while index_pointer + 1 < total_numbers and numbers[index_pointer + 1] == numbers[index_pointer] + 1:
index_pointer += 1
# We format the range based on whether it is a single number or a sequence
if start_pointer == index_pointer:
summary_results.append(str(numbers[start_pointer]))
else:
range_string = f"{numbers[start_pointer]}->{numbers[index_pointer]}"
summary_results.append(range_string)
index_pointer += 1
return summary_resultsThe optimal strategy groups consecutive numbers together by scanning through the sorted sequence from start to finish. It tracks the beginning and end of each unbroken chain of numbers, packaging them into neat ranges as it goes.
Here's how the algorithm would work step-by-step:
def summary_ranges(sorted_numbers):
collected_ranges = []
array_index = 0
total_elements = len(sorted_numbers)
while array_index < total_elements:
start_of_chain = sorted_numbers[array_index]
end_of_chain = start_of_chain
# We scan ahead to find the boundary where the consecutive sequence breaks.
while array_index + 1 < total_elements and sorted_numbers[array_index + 1] == sorted_numbers[array_index] + 1:
array_index += 1
end_of_chain = sorted_numbers[array_index]
# A single number range is formatted differently than a multi-number range.
if start_of_chain == end_of_chain:
collected_ranges.append(str(start_of_chain))
else:
collected_ranges.append(f"{start_of_chain}->{end_of_chain}")
array_index += 1
return collected_ranges| Case | How to Handle |
|---|---|
| Empty array input | Check for null or empty array at the start and return an empty list immediately. |
| Array with a single element | The iteration loop finishes and appends the single element formatted as a string to the result list. |
| Array containing negative numbers and zero | Arithmetic checks like nums[j] + 1 == nums[j + 1] work correctly across zero and into negative ranges. |
| Array with no continuous ranges | Every element forms its own standalone range and is formatted individually as a single-number string. |
| Array with all elements forming a single continuous range | The pointers expand across the entire array until the end, producing a single range string from start to end. |
| Large scale arrays near memory and time limits | A single-pass linear scan approach processes the array in O(n) time and O(1) extra space excluding the output. |
| Integer boundary values like Integer.MIN_VALUE and Integer.MAX_VALUE | Comparing nums[j] + 1 can cause integer overflow in languages like Java, so use long casting or check equality with nums[j + 1] - 1 instead. |