easyArrays & HashingPure DSA~13 min
Compress ID Ranges
A sync log lists the sorted, unique record IDs that changed. To shrink the payload, collapse consecutive runs into compact ranges like "4->7".
Problem
Given a sorted array of distinct integers nums, return the smallest sorted list of ranges that covers all the numbers exactly. A single number a is shown as "a"; a run a..b as "a->b".
Input
A sorted array of distinct integers nums (0 ≤ len ≤ 20), 32-bit values.
Output
A list of range strings covering every element.
Constraints
- Input is sorted and has no duplicates
- Consecutive means differ by exactly 1
- Beware int overflow on the difference; values may be near limits
Examples
Example 1
Input
nums = [0,1,2,4,5,7]
Output
["0->2","4->5","7"]
Runs 0–2 and 4–5 collapse; 7 stands alone.
Example 2
Input
nums = [0,2,3,4,6,8,9]
Output
["0","2->4","6","8->9"]
Isolated values stay single; runs collapse.