EasyNo. 1313

Decompress Run-Length Encoded List — LeetCode 1313 Python Solution

Topics
Array
Reading time
2 min

The 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).

Example

Input
nums = [1,2,3,4]
Output
[2,4,4,4]
Explanation
The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].

Python solution

PythonSolution
class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        return [nums[i + 1] for i in range(0, len(nums), 2) for _ in range(nums[i])]

Complexity

Time
O(n)where n is the length of the array nums
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

Frequently asked questions

How hard is LeetCode 1313. Decompress Run-Length Encoded List?

LeetCode 1313. Decompress Run-Length Encoded List is rated Easy on LeetCode.

What is the time complexity of LeetCode 1313. Decompress Run-Length Encoded List?

The Python solution on this page runs in O(n), where n is the length of the array \textit{nums}.

What is the space complexity of LeetCode 1313. Decompress Run-Length Encoded List?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 1313. Decompress Run-Length Encoded List cover?

LeetCode 1313. Decompress Run-Length Encoded List is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.