Python 1313.py
class Solution:
def decompressRLElist(self, nums: List[int]) -> List[int]:
for i in range(0, len(nums), 2):
freq, val = nums[i], nums[i + 1]
for j in range(freq):
yield val
Difficulty
and has an acceptance rate of about 86% (300k+ submissions).
Typical patterns to solve this problem:
- Array
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a Solution class with a single method decompressRLElist. The method receives a list of integers that encodes a run-length encoded (RLE) sequence: each pair of consecutive elements represents a frequency and a value. The method expands the encoding by emitting each value the specified number of times. It does so using a generator, allowing the caller to iterate over the decompressed elements without constructing an intermediate list.
Key Steps
Iterating over the encoded pairs
for i in range(0, len(nums), 2):
freq, val = nums[i], nums[i + 1]
The outer loop steps through the input list with a stride of two, ensuring that each iteration processes exactly one frequency/value pair. By extracting freq and val into local variables, the code avoids repeated indexing inside the inner loop.
Emitting values according to frequency
for j in range(freq):
yield val
The inner loop runs freq times, yielding the current val on each pass. Because the method uses yield, it produces a lazy sequence rather than materializing the entire decompressed list at once.
Edge Cases Handled
When a frequency is zero, the inner range(freq) produces an empty sequence, so the yield statement is never executed for that pair. This means the method correctly skips values that are meant to appear zero times without raising an error.
If the input list is empty, len(nums) is zero, the outer range produces no indices, and the generator finishes immediately, yielding no elements. The implementation therefore gracefully handles an empty encoding.
The code assumes that the input length is even, as required by the RLE format. When the length is even but the final pair contains a frequency of zero, the generator still terminates correctly after processing all pairs.
Performance Optimizations
The most significant optimization is the use of a generator (yield). By emitting each decompressed element on demand, the method avoids allocating a potentially large list that holds the entire output. This reduces memory consumption, especially for inputs with large frequencies.
Iterating with range(0, len(nums), 2) eliminates the need for additional index calculations or slicing, keeping the loop overhead minimal. Storing freq and val in local variables further reduces attribute lookup costs inside the inner loop.
Overall, the design emphasizes low-memory operation and straightforward iteration, making it well-suited for scenarios where the decompressed sequence is consumed incrementally.
ELI5 - Simplified Explanation
Imagine that this code is like a magical recipe for building a line of building blocks. The list of numbers provided is a special set of instructions where every two numbers tell a story. The first number in each pair is the frequency, which tells how many times to pick up a specific block. The second number is the value, which tells which color or shape that block should be.
The first part of the code looks at the list and jumps through it two steps at a time to find these pairs. Once a pair is picked out, the code looks at the frequency and begins a smaller loop. This inner loop performs the action of placing the block down over and over again until the amount matches the frequency. Every single time a block is placed, it is added to a new train of blocks that grows longer and longer. This keeps happening until every pair in the original list has been used to create the final, expanded line of blocks.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||||
| www | AlgoMonster | C++ | Python | Java | TS | ||||||
| WalkCCC | C++ | Python | Java | ||||||||
| leetcode.ca | C++ | Python | Java | Go | C | Rust | TS | ||||
| Progiez | C++ | Python | Java | ||||||||
| github | BigEggStudy | C# | |||||||||
| black-shadows | C++ | ||||||||||
| chandansgowda | Python | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| everthis | JS | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JS | ||||||||||
| Krazune | C | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| sonugiri1043 | C++ | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java | ||||||||||
| YuriSpiridonov | Python | ||||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||||
| WalkCCC | C++, Python, Java | ||||||||||
| leetcode.ca | C, C++, Go, Java, Python, Rust, TypeScript | ||||||||||
| Progiez | C++, Java, Python | ||||||||||
| BigEggStudy | C# | ||||||||||
| black-shadows | C++, C++ | ||||||||||
| chandansgowda | Python | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| everthis | JavaScript | ||||||||||
| fartem | Ruby | ||||||||||
| fsq | C++ | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JavaScript | ||||||||||
| Krazune | C | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| sonugiri1043 | C++ | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java | ||||||||||
| YuriSpiridonov | Python |
Videos
Similar Problems
- problem 443: String Compression
Solved On The Same Day
- problem 1678: Goal Parser Interpretation
- problem 2652: Sum Multiples
- problem 2859: Sum of Values at Indices With K Set Bits
- problem 2125: Number of Laser Beams in a Bank
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality