LeetCode 1313: Decompress Run-Length Encoded List Solution

Master LeetCode problem 1313 (Decompress Run-Length Encoded List), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1313. Decompress Run-Length Encoded List

Easy

Problem Explanation

Explanation

To solve this problem, we iterate through the input list in pairs, where each pair represents the frequency and value of elements to be decompressed. We then generate the decompressed list based on these pairs.

  • Initialize an empty list to store the decompressed elements.
  • Iterate through the input list in pairs.
  • For each pair [freq, val], add freq number of val to the decompressed list.
  • Continue this process until all pairs are processed.
  • Return the decompressed list as the final result.

Time Complexity:

The time complexity of this solution is O(n), where n is the number of elements in the input list.

Space Complexity:

The space complexity is also O(n) since we are storing the decompressed elements in a separate list.

Solution Code

class Solution {
    public int[] decompressRLElist(int[] nums) {
        List<Integer> decompressed = 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++) {
                decompressed.add(val);
            }
        }
        
        int[] result = new int[decompressed.size()];
        for (int i = 0; i < decompressed.size(); i++) {
            result[i] = decompressed.get(i);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1313 (Decompress Run-Length Encoded List)?

This page provides optimized solutions for LeetCode problem 1313 (Decompress Run-Length Encoded List) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1313 (Decompress Run-Length Encoded List)?

The time complexity for LeetCode 1313 (Decompress Run-Length Encoded List) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1313 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1313 in Java, C++, or Python.

Back to LeetCode Solutions