Python 1720.py
class Solution:
def decode(self, encoded: List[int], first: int) -> List[int]:
yield first
for e in encoded:
first ^= e
yield first
Difficulty
and has an acceptance rate of about 87% (200k+ submissions).
Typical patterns to solve this problem:
- Array
- Bit Manipulation
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 presented implementation defines a Solution class with a single method decode. The method receives an integer list encoded and an integer first. It produces the original array that was transformed into encoded by successive XOR operations. The method is written as a generator, yielding each element of the decoded sequence on demand rather than constructing a full list up front.
Key Steps
The first operation in the method is to emit the initial value supplied by the caller. This establishes the starting point for the reconstruction process.
yield first
The core of the algorithm is a loop that iterates over every element e in the encoded list. Inside the loop the current first value is updated by XOR-ing it with the current encoded element. The updated value represents the next element of the original array and is yielded immediately.
for e in encoded:
first ^= e
yield first
The use of the ^= operator modifies first in place, ensuring that the variable always holds the most recent decoded element. By yielding after each update, the method provides a lazy sequence that can be consumed incrementally.
Edge Cases Handled
When encoded is empty, the loop body is never executed. The generator still yields the initial first value, correctly representing an original array consisting of a single element. This behavior gracefully handles the minimal input size.
If encoded contains a single element, the loop runs exactly once. The XOR operation produces the second element of the original array, which is yielded after the initial element. The method therefore reconstructs a two-element array without any special branching.
The algorithm works for any integer values, including large positive numbers, zero, and negative numbers, because the XOR operator is defined for the full range of Python integers. No overflow concerns arise, as Python integers have arbitrary precision.
Performance Optimizations
The implementation processes the input in a single pass, updating the running value and emitting results on the fly. By using a generator (yield) rather than building an intermediate list, it avoids allocating additional memory proportional to the length of the input. This makes the method suitable for very large encoded sequences where memory consumption would otherwise be a limiting factor.
In-place modification of the first variable with the ^= operator eliminates the need for a temporary variable to hold the intermediate XOR result. This reduces the number of Python objects created during execution, contributing to lower overhead per iteration.
Overall, the design leverages Python’s iterator protocol to achieve a streamlined, memory-efficient reconstruction of the original array.
ELI5 - Simplified Explanation
Imagine you have a row of secret number blocks. The first block is a special gift you already know. The other blocks are hidden inside mysterious boxes. To find the next number, you take the number you just discovered and play a little magic trick with it and the secret box. This magic trick is like a flip that changes the number. You keep doing this trick over and over, going down the line, until all the hidden numbers are revealed one by one. Once you finish flipping and discovering, you have a whole new line of numbers that were waiting to be found.
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 | Python | C++ | Java | TS | |||||
| WalkCCC | Python | C++ | Java | |||||||
| leetcode.ca | Python | C++ | Java | Go | TS | |||||
| Progiez | Python | C++ | Java | |||||||
| github | black-shadows | C++ | ||||||||
| chandansgowda | Python | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JS | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| halfrost | Go | |||||||||
| JoshCrozier | JS | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| suxrobGM | C# | |||||||||
| varunu28 | Java | |||||||||
| YuriSpiridonov | Python | |||||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||||
| WalkCCC | C++, Python, Java | |||||||||
| leetcode.ca | C++, Go, Java, Python, TypeScript | |||||||||
| Progiez | C++, Java, Python | |||||||||
| black-shadows | C++, C++ | |||||||||
| chandansgowda | Python | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JavaScript | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| halfrost | Go | |||||||||
| JoshCrozier | JavaScript | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| suxrobGM | C# | |||||||||
| varunu28 | Java | |||||||||
| YuriSpiridonov | Python |
Videos
Similar Problems
- problem 2433: Find The Original Array of Prefix Xor
Solved On The Same Day
- problem 1379: Find a Corresponding Node of a Binary Tree in a Clone of That Tree
- problem 2160: Minimum Sum of Four Digit Number After Splitting Digits
- problem 2181: Merge Nodes in Between Zeros
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