DSA · Arrays · #174
Summary Ranges
Module 48 · difficulty 2/5·⏱ 30:00starts on first keystroke
You are given a **sorted unique** integer array `nums`. Return the smallest sorted list of ranges that **exactly** covers all the numbers in the array. That is, each element of `nums` is covered by exactly one of the ranges, and there is no number `x` in any range such that `x` is not in `nums`. Each range `[a, b]` in the list is formatted as: - `"a->b"` if `a != b` - `"a"` if `a == b` Implement the function `summaryRanges(nums)` returning an array of strings.
Examples
nums = [0,1,2,4,5,7]→["0->2","4->5","7"]— 0,1,2 form 0->2; 4,5 form 4->5; 7 stands alone.nums = [0,2,3,4,6,8,9]→["0","2->4","6","8->9"]— Gaps after 0, after 4, and after 6 break the ranges.nums = []→[]— Empty input yields no ranges.
Constraints
- · 0 <= nums.length <= 20
- · -2^31 <= nums[i] <= 2^31 - 1
- · All values in nums are unique.
- · nums is sorted in strictly ascending order.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.