Decompress Run-Length Encoded List
The given problem deals with run-length encoding, where pairs of elements [freq, val]
define how many times the value val
should appear consecutively in the decompressed list. Here’s a solution:
|
|
Explanation
Initialization: We initialize an empty list
result
to store the decompressed values.Iterating through Pairs: We iterate through the
nums
list with a step size of 2 (range(0, len(nums), 2)
), so that we can consider pairs of elements[freq, val]
at each iteration.Decompressing Each Pair: Inside the loop, we extract the frequency
freq
and valueval
from the current pair. We then create a sublist[val] * freq
, which means the valueval
repeatedfreq
times, and append it to theresult
list.Returning the Result: After iterating through all pairs, we return the
result
list, which represents the decompressed version of the given run-length encoded list.