Compact runs
EasyOnline Assessment
Run-length encode a list of integers.
A run is a maximal block of consecutive equal values. Replace each run with a pair [value, count], keeping the runs in their original order. The same value appearing in two separate places forms two separate runs.
Input: values, a list of integers.
Output: a list of [value, count] pairs. An empty list returns [].
Example 1
- Input:
- [[2,2,4,2]]
- Output:
- [[2,2],[4,1],[2,1]]
- Explanation
- The two leading 2s form one run, then a single 4, then a single 2. The last 2 is not merged with the first run because a 4 sits between them.
Example 2
- Input:
- [[0,0,0,5,5,0]]
- Output:
- [[0,3],[5,2],[0,1]]
- Explanation
- Three 0s, then two 5s, then one 0: the trailing 0 is a new run, not part of the first.
Example 3
- Input:
- [[1,2,3]]
- Output:
- [[1,1],[2,1],[3,1]]
- Explanation
- No value repeats back to back, so every element is its own run of length 1.
Constraints
0 <= values.length <= 10,000-1,000,000 <= values[i] <= 1,000,000