LeetCode Problem Workspace

Decompress Run-Length Encoded List

Decompress a run-length encoded list by expanding each pair into repeated values based on frequency.

category

1

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Array-driven solution strategy

bolt

Answer-first summary

Decompress a run-length encoded list by expanding each pair into repeated values based on frequency.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array-driven solution strategy

Try AiBox Copilotarrow_forward

The problem asks you to decompress a list encoded in a run-length format. The array is given with pairs of frequency and value, and you must expand each pair into the corresponding repeated values. This task can be solved efficiently using an array-driven approach, handling each pair in sequence and constructing the final decompressed list.

Problem Statement

You are given a list of integers, nums, representing a list compressed with run-length encoding. Each adjacent pair of elements [freq, val] = [nums[2 i], nums[2 i+1]] (with i >= 0) indicates that there are 'freq' instances of the value 'val'. The goal is to decompress the list and return the expanded sequence.

Return the decompressed list by expanding the given pairs. For each pair [freq, val], repeat 'val' exactly 'freq' times and concatenate all sublists together to form the final output.

Examples

Example 1

Input: nums = [1,2,3,4]

Output: [2,4,4,4]

The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2]. The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4]. At the end the concatenation [2] + [4,4,4] is [2,4,4,4].

Example 2

Input: nums = [1,1,2,3]

Output: [1,3,3]

Example details omitted.

Constraints

  • 2 <= nums.length <= 100
  • nums.length % 2 == 0
  • 1 <= nums[i] <= 100

Solution Approach

Iterate and Expand

Iterate through the array in steps of two. For each pair [freq, val], append 'val' repeated 'freq' times to a result list. This ensures that you directly construct the decompressed array in an optimal manner.

Use List Operations

Utilize Python's list multiplication feature to repeat values. This can significantly reduce the complexity of the approach when dealing with larger arrays, as it allows you to handle the repetition of 'val' in a compact and efficient manner.

Minimize Extra Space

Focus on minimizing space by directly modifying the result list rather than creating additional intermediate structures. This reduces memory usage, particularly for larger inputs.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity depends on how many elements are being decompressed, but in the worst case, it's proportional to the number of pairs and their respective frequency values. Space complexity is similarly dependent on the output list's length.

What Interviewers Usually Probe

  • Check if the candidate understands the run-length encoding and how to iterate through pairs.
  • Observe if the candidate uses list operations efficiently to minimize overhead.
  • Assess whether the candidate avoids excessive space usage by directly modifying the result.

Common Pitfalls or Variants

Common pitfalls

  • Incorrectly handling the case where nums contains an odd number of elements or non-even pairs.
  • Using inefficient techniques like repeated manual loops instead of list operations for value repetition.
  • Not ensuring that the result list is built in an optimal way, possibly leading to high memory consumption.

Follow-up variants

  • Given an input of varying length, test if the solution scales with different amounts of input data.
  • Test the case with large frequency values to see if the solution can handle arrays with high expansion factors.
  • Introduce more complex encoding schemes with additional data types or constraints to test adaptability.

FAQ

What is a run-length encoding?

Run-length encoding is a form of data compression where consecutive elements of the same value are stored as a single pair: [frequency, value].

How do I solve the Decompress Run-Length Encoded List problem?

You solve this problem by iterating through the array in pairs, expanding each pair [freq, val] by repeating 'val' 'freq' times, and appending it to the result list.

What are the time and space complexities for this problem?

The time complexity is O(n) where n is the length of the decompressed array. The space complexity is O(n) as well, due to the need to store the expanded list.

How can I optimize my solution for large inputs?

Use Python's list multiplication feature to efficiently repeat values, and minimize memory consumption by directly appending to the result list.

What should I watch out for when solving this problem?

Be careful with handling cases where the input list might contain non-even pairs or large frequency values that lead to expanded lists.

terminal

Solution

Solution 1: Simulation

We can directly simulate the process described in the problem. Traverse the array $\textit{nums}$ from left to right, each time taking out two numbers $\textit{freq}$ and $\textit{val}$, then repeat $\textit{val}$ $\textit{freq}$ times, and add these $\textit{freq}$ $\textit{val}$s to the answer array.

1
2
3
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])]
Decompress Run-Length Encoded List Solution: Array-driven solution strategy | LeetCode #1313 Easy