Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1313. Decompress Run-Length Encoded List - Leetcode Solution

Code Implementation

class Solution:
    def decompressRLElist(self, nums):
        result = []
        for i in range(0, len(nums), 2):
            freq = nums[i]
            val = nums[i+1]
            result.extend([val] * freq)
        return result
      
class Solution {
public:
    vector<int> decompressRLElist(vector<int>& nums) {
        vector<int> result;
        for (int i = 0; i < nums.size(); i += 2) {
            int freq = nums[i];
            int val = nums[i+1];
            for (int j = 0; j < freq; ++j) {
                result.push_back(val);
            }
        }
        return result;
    }
};
      
class Solution {
    public int[] decompressRLElist(int[] nums) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i += 2) {
            int freq = nums[i];
            int val = nums[i+1];
            for (int j = 0; j < freq; ++j) {
                result.add(val);
            }
        }
        int[] arr = new int[result.size()];
        for (int i = 0; i < arr.length; ++i) {
            arr[i] = result.get(i);
        }
        return arr;
    }
}
      
var decompressRLElist = function(nums) {
    const result = [];
    for (let i = 0; i < nums.length; i += 2) {
        let freq = nums[i];
        let val = nums[i+1];
        for (let j = 0; j < freq; ++j) {
            result.push(val);
        }
    }
    return result;
};
      

Problem Description

You are given a list called nums that represents a run-length encoded list. The encoding works as follows: for every pair of elements nums[2*i] and nums[2*i+1], the first element (freq) tells you how many times to repeat the second element (val) in the decompressed list.

Your task is to return the decompressed list by expanding each [freq, val] pair accordingly. Assume nums will always have an even length, and every freq is a non-negative integer.

Key constraints:

  • Each pair is processed in order; do not skip or reorder elements.
  • There is always one valid decompressed list for any valid input.
  • Do not reuse or skip any elements in nums.

Thought Process

When approaching this problem, the first step is to recognize that the input is a sequence of pairs, where each pair tells you how many times to repeat a value. The most straightforward (brute-force) solution would be to iterate through each pair and, for each, append the value to a result list as many times as specified by the frequency.

However, we might wonder if there are more efficient ways—perhaps by pre-allocating space if we know the total length, or using language features to repeat values quickly. But since the problem is simple and the input size is manageable, a direct approach is both clear and efficient enough.

The main conceptual shift is to recognize that there is no need for complex data structures or algorithms. Instead, focus on cleanly iterating through the list in steps of two, extracting freq and val at each step, and building up the answer.

Solution Approach

Let's break down the solution into clear steps:

  1. Initialize an empty result list: This will hold the decompressed numbers.
  2. Iterate through nums in steps of two: Each step processes one [freq, val] pair.
  3. For each pair, extract freq and val: freq = nums[i], val = nums[i+1].
  4. Repeat val freq times: Append val to the result list freq times. In many languages, you can use a loop or a built-in function to do this efficiently.
  5. Return the result list: After processing all pairs, return the decompressed list.

This approach is simple, readable, and leverages the fact that the input is well-formed and not excessively large.

Example Walkthrough

Let's use a sample input: nums = [2, 3, 4, 5].

  • First pair: freq = 2, val = 3. We append 3 two times: [3, 3].
  • Second pair: freq = 4, val = 5. We append 5 four times: [5, 5, 5, 5].

Concatenating the results gives: [3, 3, 5, 5, 5, 5].

At each iteration, we process a pair and expand the result accordingly, ensuring we include the correct number of repetitions for each value.

Time and Space Complexity

Brute-force approach:

  • Time complexity: O(N), where N is the total length of the decompressed list. Each value is appended exactly once.
  • Space complexity: O(N), since we must store the decompressed list.
Optimized approach:
  • Since the brute-force approach already processes each element exactly as needed, there is no significant optimization possible. All approaches must touch every output element.
  • Some language-specific tricks (like list comprehensions or built-in repeat functions) may be faster in practice, but do not change the overall complexity.

Summary

The "Decompress Run-Length Encoded List" problem is a great example of how a simple, direct approach can be both elegant and efficient. By iterating through the input in pairs and expanding each value by its frequency, we arrive at the correct answer with minimal code and clear logic. The key insight is to recognize the pattern in the input and apply a straightforward expansion, making this problem accessible even to those new to programming.