Similar Problems

Similar Problems not available

Decompress Run Length Encoded List - Leetcode Solution

Companies:

LeetCode:  Decompress Run Length Encoded List Leetcode Solution

Difficulty: Easy

Topics: array  

The Decompress Run Length Encoded List problem on LeetCode can be solved using the following steps:

  1. Create an empty result list to store the decompressed list.
  2. Loop through the input list in pairs starting from index 0. Each pair represents a run of values to be repeated, with the first element representing the frequency and the second element representing the value.
  3. For each pair, append the value to the result list frequency times.
  4. Return the result list.

Here is the Python code to implement this solution:

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:
        res = []
        for i in range(0, len(nums) - 1, 2):
            freq, val = nums[i], nums[i+1]
            res += [val] * freq
        return res

In this code, we initialize an empty list called res to store the decompressed list. We then loop through the input list nums in pairs from index 0 using the range function with a step of 2. For each pair, we extract the frequency and value using the index values i and i+1 respectively. We then use the * operator to append the value to the result list freq times. Finally, we return the result list res.

For example, if nums = [1, 2, 3, 4], the function call decompressRLElist(nums) returns [2, 4, 4, 4]. The first pair [1, 2] indicates that the value 2 should be repeated once, while the second pair [3, 4] indicates that the value 4 should be repeated three times. The resulting decompressed list is [2, 4, 4, 4].

Decompress Run Length Encoded List Solution Code

1