Decompress Run-Length Encoded List - Problem
We are given a list nums of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
Return the decompressed list.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4]
›
Output:
[2,4,4,4]
💡 Note:
First pair [1,2]: frequency 1, value 2 → add 2 once. Second pair [3,4]: frequency 3, value 4 → add 4 three times. Result: [2,4,4,4]
Example 2 — Multiple Pairs
$
Input:
nums = [1,1,2,3]
›
Output:
[1,3,3]
💡 Note:
First pair [1,1]: frequency 1, value 1 → add 1 once. Second pair [2,3]: frequency 2, value 3 → add 3 twice. Result: [1,3,3]
Example 3 — Zero Frequency
$
Input:
nums = [2,1,3,2]
›
Output:
[1,1,2,2,2]
💡 Note:
First pair [2,1]: frequency 2, value 1 → add 1 twice. Second pair [3,2]: frequency 3, value 2 → add 2 three times. Result: [1,1,2,2,2]
Constraints
- 2 ≤ nums.length ≤ 100
- nums.length % 2 == 0
- 1 ≤ nums[2 * i] ≤ 100
- 1 ≤ nums[2 * i + 1] ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code