algoadvance

You are given a list nums where each pair of elements in the list represent [frequency, value] sequence. Your task is to return the decompressed version of the list where each value appears frequency times consecutively in the list.

Example:

Clarifying Questions

  1. Size Constraints: What is the maximum length of the input list nums?
    • The length of nums is guaranteed to be even, and it’s between 2 and 100.
  2. Element Constraints: What are the possible values for frequency and value?
    • Frequency values are between 1 and 100.
    • Value elements are between 1 and 100.

Strategy

  1. Initialize an empty list:
    • This will be used to store the decompressed values.
  2. Iterate through the nums list:
    • Loop through the list in steps of 2.
    • For each pair of elements, extract the frequency and value.
  3. Extend the list:
    • Use the extend method to add the value repeated frequency times to the resulting list.
  4. Return the decompressed list:
    • After processing all pairs, return the resulting list.

Time Complexity

Code

def decompressRLElist(nums):
    decompressed_list = []
    for i in range(0, len(nums), 2):
        frequency = nums[i]
        value = nums[i + 1]
        decompressed_list.extend([value] * frequency)
    return decompressed_list

Example Run

Let’s test the implementation with one of the provided examples:

Example 1:

Explanation:

So, the decompressed list is [2, 4, 4, 4].

Using the provided implementation, this would effectively solve the given problem.

Try our interview co-pilot at AlgoAdvance.com