Decompress Run-Length Encoded List

You are given a list nums that run-length encodes another list. Read it as adjacent pairs [freq, val] = [nums[2*i], nums[2*i+1]]: each pair stands for freq copies of the value val. Concatenate the expanded pairs from left to right and return the decompressed list.

30:00

Decompress Run-Length Encoded List
easy
Free written solution
Topics
Companies

You are given a list nums that run-length encodes another list. Read it as adjacent pairs [freq, val] = [nums[2i], nums[2i+1]]: each pair stands for freq copies of the value val. Concatenate the expanded pairs from left to right and return the decompressed list.

Example 1:
Input: {"nums":[1,2,3,4]}
Output: [2,4,4,4]
Constraints:
  • 2≤nums.length≤1002 \leq \text{nums.length} \leq 100

  • nums.length mod 2=0\text{nums.length} \bmod 2 = 0

  • 1≤nums[i]≤1001 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,3,4]}

nums holds 2 pairs of [freq, val]. Start with an empty result and expand each pair in order.

encoded nums, read two at a time: freq then val

1
2
3
4

no pair read yet

result (0 values)

[ ]

0 of 2 pairs expanded; each color is one pair's run

Variables
VariableValue
len(result)0
DepthFunction Call
Stack empty
0/5