Problem Number: 228 Difficulty: Easy Category: Array LeetCode Link: https://leetcode.com/problems/summary-ranges/
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] should be output as:
"a->b"ifa != b"a"ifa == 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-2^31 <= nums[i] <= 2^31 - 1- All the values of
numsare unique. numsis sorted in ascending order.
I used a Two Pointers approach to identify consecutive ranges. The key insight is to use two pointers (i, j) to track the start and end of each consecutive range.
Algorithm:
- Handle edge cases (empty array, single element)
- Initialize two pointers i and j at start
- Iterate through array starting from second element
- If current element is consecutive (nums[h-1] + 1 == nums[h]), extend range
- Otherwise, add current range to result and reset pointers
- Handle the last range after loop ends
- Return list of formatted ranges
The solution uses two pointers to identify and format consecutive ranges. See the implementation in the solution file.
Key Points:
- Uses two pointers to track range boundaries
- Handles single element ranges (i == j)
- Formats ranges as "a->b" or "a"
- Handles edge cases properly
- Processes last range after loop
Time Complexity: O(n)
- Single pass through the array
- Each element is processed once
- Total: O(n)
Space Complexity: O(n)
- Result list can contain up to n ranges
- Each range is a string
- Total: O(n)
-
Two Pointers: Using i and j pointers to track range boundaries.
-
Consecutive Check: Checking if nums[h-1] + 1 == nums[h] for consecutive elements.
-
Range Formatting: Single element ranges are formatted as "a", multiple elements as "a->b".
-
Edge Cases: Handling empty arrays and single element arrays.
-
Last Range: Processing the last range after the loop ends.
-
Sorted Input: Leveraging the fact that input is sorted.
-
Wrong Range Logic: Initially might use wrong logic for identifying ranges.
-
Missing Edge Cases: Not handling empty arrays or single elements properly.
-
Wrong Formatting: Not formatting ranges correctly.
-
Last Range: Forgetting to process the last range after the loop.
- Merge Intervals (Problem 56): Merge overlapping intervals
- Insert Interval (Problem 57): Insert new interval into sorted intervals
- Missing Ranges (Problem 163): Find missing ranges
- Range Sum Query (Problem 303): Calculate sum in range
- Single Pointer: Use single pointer with range tracking - O(n) time
- Stack-based: Use stack to track ranges - O(n) time, O(n) space
- Recursive: Use recursion to process ranges - O(n) time, O(n) space
- Wrong Range Logic: Using incorrect logic for identifying consecutive ranges.
- Missing Edge Cases: Not handling empty arrays or single elements.
- Wrong Formatting: Not formatting ranges correctly.
- Last Range: Forgetting to process the last range after the loop.
- Complex Logic: Overcomplicating the range identification.
Note: This is a range processing problem that demonstrates efficient consecutive range identification with two pointers.