Problem 293654 · easy · Level 02 Linear Data Structures

Run-Length Encode a List

arrays · run-length encoding · iteration

Given a list nums, compress it into a list of [value, count] pairs: each maximal run of consecutive equal values becomes one pair. Runs of the same value that are separated by other values are not merged.

Examples

Input:  nums = [1, 1, 2, 3, 3, 3]
Output: [[1, 2], [2, 1], [3, 3]]

Input:  nums = [1, 1, 2, 2, 1, 1]
Output: [[1, 2], [2, 2], [1, 2]]
Explanation: the two runs of 1 are separated, so they stay separate pairs.

Constraints

  • 0 <= len(nums) <= 10**5
  • Return a new list of two-element lists (not tuples).
  • Target: O(n) time.

Goals

  • Detect where a run of equal values ends
  • Emit [value, count] pairs in order
  • Handle the empty list and single-element runs
Starting Python…