Skip to content
BytePatterns

Condense Values Into Ranges

EasyIntervals#intervals#single-pass~15m

Problem

You are given a sorted list of distinct integers. Describe it as the shortest possible list of runs of consecutive integers, in order. Write a run as "a->b" when it covers more than one value, and as just "a" when it holds a single value.

Examples

Input:  nums = [0, 1, 2, 4, 5, 7]
Output: ["0->2", "4->5", "7"]
Input:  nums = [-3, -1, 0]
Output: ["-3", "-1->0"]
Why:    negative values follow the same rule
Input:  nums = []
Output: []
Why:    edge case, no values means no runs

Hints

0 / 3

Stuck on the idea rather than the code? Interval Basics & Sorting covers it.