LeetCode 228: Summary Ranges Solution in Python – A Step-by-Step Guide
Imagine turning a list of numbers into a compact summary of consecutive ranges—that’s the essence of LeetCode 228: Summary Ranges! This easy-level problem challenges you to take a sorted array of integers and return a list of strings representing ranges of consecutive numbers. Using Python, we’ll explore two solutions: the Best Solution (a single-pass method) for its simplicity and efficiency, and an Alternative Solution (a range-tracking method with explicit checks) for a different perspective. With beginner-friendly breakdowns, detailed examples, and clear code, this guide will help you master range summarization and sharpen your coding skills. Let’s summarize those ranges!
What Is LeetCode 228: Summary Ranges?
In LeetCode 228: Summary Ranges, you’re given a sorted unique integer array, and your task is to summarize it into a list of strings where consecutive numbers are grouped as ranges (e.g., "0->2") and single numbers are listed alone (e.g., "4"). This problem tests your ability to process sequences, differing from calculator challenges like LeetCode 227: Basic Calculator II.
Problem Statement
- Input: A sorted array of unique integers nums.
- Output: A list of strings representing ranges.
- Rules:
- For a single number: return it as a string (e.g., "5").
- For consecutive numbers: return "start->end" (e.g., "1->3").
Constraints
- Array length: 0 to 20.
- Numbers: -2^31 to 2^31 - 1.
- Array is sorted and contains no duplicates.
Examples
- Input: [0,1,2,4,5,7]
- Output: ["0->2","4->5","7"] (0,1,2 is a range; 4,5 is a range; 7 is alone).
- Input: [0,2,3,4,6,8,9]
- Output: ["0","2->4","6","8->9"].
- Input: []
- Output: [] (empty array).
Understanding the Problem: Summarizing Ranges
To solve LeetCode 228: Summary Ranges in Python, we need to identify consecutive sequences in a sorted array and format them as strings. This isn’t about tree manipulation like LeetCode 226: Invert Binary Tree—it’s about sequence processing. We’ll use two approaches: 1. Best Solution (Single-Pass): O(n) time, O(1) space (excluding output)—clean and efficient. 2. Alternative Solution (Range-Tracking with Explicit Checks): O(n) time, O(1) space—more explicit logic.
Let’s start with the best solution.
Best Solution: Single-Pass Approach for Summary Ranges
Why This Is the Best Solution
The single-pass approach is our top choice for LeetCode 228 because it scans the array once, tracking range starts and ends with minimal variables, producing a concise and efficient solution. It’s perfect for beginners and pros alike due to its simplicity and performance.
How It Works
- Track the start of a range.
- For each number, check if it’s consecutive with the previous (differs by 1).
- When a range ends (gap or end of array), format it as "start->end" (if longer than 1) or "start" (if single).
- Append to result and update the start.
Step-by-Step Example
Example 1: [0,1,2,4,5,7]
- Result: [], start = 0.
- 0: Start range at 0.
- 1: 1-0 = 1, consecutive.
- 2: 2-1 = 1, consecutive.
- 4: 4-2 = 2, gap. End range 0->2, append "0->2", start = 4.
- 5: 5-4 = 1, consecutive.
- 7: 7-5 = 2, gap. End range 4->5, append "4->5", start = 7.
- End: 7 alone, append "7".
- Output: ["0->2","4->5","7"].
Example 2: [0,2,3]
- Result: [], start = 0.
- 0: Start at 0.
- 2: 2-0 = 2, gap. Append "0", start = 2.
- 3: 3-2 = 1, consecutive.
- End: Append "2->3".
- Output: ["0","2->3"].
Code with Detailed Line-by-Line Explanation
Here’s the Python implementation:
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
# Line 1: Handle empty array
if not nums:
return []
# Line 2: Initialize result and range start
result = []
start = nums[0]
# Line 3: Process each number
for i in range(1, len(nums)):
# Line 4: Check for gap
if nums[i] - nums[i-1] > 1:
# Line 5: End current range
if start == nums[i-1]:
result.append(str(start)) # Single number
else:
result.append(f"{start}->{nums[i-1]}") # Range
# Line 6: New range starts
start = nums[i]
# Line 7: Handle the last range
if start == nums[-1]:
result.append(str(start)) # Single number
else:
result.append(f"{start}->{nums[-1]}") # Range
# Line 8: Return the result
return result
- Time Complexity: O(n) – single pass through the array.
- Space Complexity: O(1) – excluding output space.
Alternative Solution: Range-Tracking Approach with Explicit Checks
Why an Alternative Approach?
The range-tracking approach uses explicit start and end pointers, checking ranges step-by-step. It’s a good alternative if you prefer a more granular control over range detection or want a different way to visualize the process.
How It Works
- Use two pointers: start and curr.
- Compare adjacent numbers for consecutiveness.
- When a gap is found or the end is reached, format the range and move the start.
Step-by-Step Example
Example: [0,1,2,4,5,7]
- Result: [], start = 0, curr = 0.
- 0->1: Consecutive, curr = 1.
- 1->2: Consecutive, curr = 2.
- 2->4: Gap, append "0->2", start = 4, curr = 4.
- 4->5: Consecutive, curr = 5.
- 5->7: Gap, append "4->5", start = 7, curr = 7.
- End: Append "7".
- Output: ["0->2","4->5","7"].
Code for Range-Tracking Approach
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
if not nums:
return []
result = []
start = nums[0]
curr = start
for i in range(1, len(nums)):
if nums[i] == curr + 1:
curr = nums[i] # Extend range
else:
# End current range
if start == curr:
result.append(str(start))
else:
result.append(f"{start}->{curr}")
start = nums[i]
curr = start
# Handle last range
if start == curr:
result.append(str(start))
else:
result.append(f"{start}->{curr}")
return result
- Time Complexity: O(n) – processes each element once.
- Space Complexity: O(1) – excluding output.
Comparing the Two Solutions
- Best Solution (Single-Pass):
- Pros: Minimal variables, straightforward logic.
- Cons: Slightly less explicit range tracking.
- Alternative Solution (Range-Tracking):
- Pros: Clear start/end tracking, explicit checks.
- Cons: Slightly more verbose.
The single-pass method is our best for its simplicity and elegance.
Additional Examples and Edge Cases
Empty Array
- Input: []
- Output: []
- Both return empty list.
Single Number
- Input: [5]
- Output: ["5"]
- Both handle correctly.
All Gaps
- Input: [0,2,4]
- Output: ["0","2","4"]
- Both work seamlessly.
Complexity Breakdown
- Single-Pass:
- Time: O(n).
- Space: O(1) (excluding output).
- Range-Tracking:
- Time: O(n).
- Space: O(1) (excluding output).
Both are optimal in time and space.
Key Takeaways for Beginners
- Consecutiveness: Check if numbers differ by 1.
- Single-Pass: Tracks ranges in one sweep.
- Range-Tracking: Uses explicit pointers.
- Python Tip: String formatting with f"{x}->{y}"—see [Python Basics](/python/basics).
Final Thoughts: Summarize Like a Pro
LeetCode 228: Summary Ranges in Python is a fantastic exercise in sequence processing. The single-pass solution offers a sleek, efficient approach, while the range-tracking method provides explicit control. Want more array challenges? Try LeetCode 229: Majority Element II or LeetCode 26: Remove Duplicates from Sorted Array. Ready to summarize? Head to Solve LeetCode 228 on LeetCode and range it up today!