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;
};
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:
nums
.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.
Let's break down the solution into clear steps:
nums
in steps of two: Each step processes one [freq, val]
pair.
freq
and val
: freq = nums[i]
, val = nums[i+1]
.
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.
This approach is simple, readable, and leverages the fact that the input is well-formed and not excessively large.
Let's use a sample input: nums = [2, 3, 4, 5]
.
freq = 2
, val = 3
. We append 3
two times: [3, 3]
.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.
Brute-force approach:
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.