Summary Ranges
EasyTop Interview 150 IntervalsYandexVKTinkoffProblem
You are given a sorted array nums of distinct integers. Describe it with as few ranges as possible,
where each range covers a run of consecutive numbers (each one exactly 1 more than the one before).
Return the ranges in order as strings. A run of several numbers is written "a->b" (first and
last number); a run of one number is written as that number alone.
Constraints
- 1 ≤ nums.length ≤ 20
- −10⁹ ≤ nums[i] ≤ 10⁹
- nums is sorted in increasing order with no repeated values
Examples: pick one to animate
Solutions
Time: Index i only moves forward and every element is visited once, either as a run start or while extending a run.
Space: Besides the output list, only the index and the run start are stored.
Walk through the array once. Remember where a run starts, then keep moving forward while the next number is exactly one bigger. When the run stops, write it as a single number or as "start->end", then begin the next run right after it.