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 == b
Example 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 - 1
nums
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:
The brute force approach to finding summary ranges involves examining every possible grouping of consecutive numbers. We essentially check each possible start and end point for a range. It's like trying every single combination to see which ones work.
Here's how the algorithm would work step-by-step:
def summary_ranges_brute_force(numbers):
result = []
number_of_elements = len(numbers)
for start_index in range(number_of_elements):
current_range = [numbers[start_index]]
end_index = start_index
# Iterate through the remaining elements to extend the range.
while end_index + 1 < number_of_elements and \
numbers[end_index + 1] == numbers[end_index] + 1:
end_index += 1
current_range.append(numbers[end_index])
# Append the summary range to the result.
if len(current_range) > 1:
result.append(str(current_range[0]) + "->" + str(current_range[-1]))
else:
result.append(str(current_range[0]))
return result
The goal is to find continuous ranges of numbers in a list and represent them concisely. The efficient solution walks through the list once, identifying range boundaries as it goes. It avoids unnecessary revisits or comparisons, making it faster.
Here's how the algorithm would work step-by-step:
def summary_ranges(numbers):
ranges = []
if not numbers:
return ranges
start_range = numbers[0]
end_range = numbers[0]
for i in range(1, len(numbers)):
current_number = numbers[i]
# Check if the current number continues the range
if current_number == end_range + 1:
end_range = current_number
else:
# The current number breaks the range, so save the previous range
if start_range == end_range:
ranges.append(str(start_range))
else:
ranges.append(str(start_range) + "->" + str(end_range))
# Start a new range from the current number
start_range = current_number
end_range = current_number
# Add the last range
if start_range == end_range:
ranges.append(str(start_range))
else:
# Add the last range
ranges.append(str(start_range) + "->" + str(end_range))
return ranges
Case | How to Handle |
---|---|
Null or empty input array | Return an empty list to avoid NullPointerException or unexpected behavior. |
Input array with a single element | Return a list containing the single element as a string. |
Input array with two consecutive elements | Check if the elements form a range and format the output accordingly. |
Input array with a long sequence of consecutive numbers | Ensure the algorithm efficiently identifies and summarizes long ranges without excessive iteration. |
Input array with negative numbers, including a sequence of consecutive negative numbers | The algorithm should correctly handle negative numbers and their ranges. |
Input array with very large integers close to the maximum integer value | The algorithm should avoid integer overflow when checking for consecutive numbers. |
Input array with a mix of small, large, positive, and negative integers | The algorithm should handle the diverse number distribution without issue. |
Integer overflow when forming the range strings | Convert integers to long to prevent overflow during range calculations and string construction if necessary. |