Run-Length Encoding

Asked at GooglePhone screen

Reported by the problem's creator, not verified

A long list of integers with many equal neighbours is often stored as runs instead of raw values. A run is a pair [value, run_length]: run_length consecutive positions that all hold value. A stored list is an ordered list of runs that covers the whole list, and neighbouring runs never hold the same value.

Implement a class RunLength with methods for working with this format.

Examples

  1. Example 1

    Input: init = [], operations = [["encode",[[3,3,3,0,0,0,4,4]]]]

    Output: [[[3,3],[0,3],[4,2]]]

    Explanation: The vector starts with three 3s, then three 0s, then two 4s, which gives three runs.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.