Python 1689.py
class Solution:
def minPartitions(self, n: str) -> int:
result = 0
for c in n:
result = max(result, int(c))
if result == 9:
break
return result
Difficulty
and has an acceptance rate of about 90% (400k+ submissions).
Typical patterns to solve this problem:
- Greedy
- String
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 minPartitions. The method receives a numeric string n and computes the smallest number of deci-binary numbers required to sum to the original value. The core insight is that the answer equals the highest digit present in the string, because each deci-binary number can contribute at most one to any digit position. The algorithm iterates through the characters of n, tracks the maximum digit seen, and stops early when the digit 9 is encountered, since no larger digit can appear.
Key Steps
Initialization of the result accumulator
The method starts by setting a variable result to zero. This variable will hold the maximum digit value discovered during the scan.
result = 0
Iteration over each character in the input string
A for loop processes each character c in the input string n. The loop body converts the character to an integer and updates result with the larger of the current result and the digit value.
for c in n:
result = max(result, int(c))
Early termination when the maximum possible digit is found
Inside the loop, after updating result, the code checks whether result has reached 9. Since 9 is the highest single-digit decimal value, encountering it guarantees that the final answer cannot be larger. The break statement exits the loop immediately, avoiding unnecessary work on the remaining characters.
if result == 9:
break
Returning the computed maximum digit
After the loop finishes-either by exhausting the string or by breaking early-the method returns the value stored in result. This value represents the minimal number of deci-binary partitions required.
return result
Edge Cases Handled
When the input string contains only the digit 0, the conversion int('0') yields 0, and the max operation leaves result unchanged at its initial value. Consequently, the method correctly returns 0, indicating that no partitions are needed for a value of zero.
If the string is empty, the loop body never executes, and result remains 0. The method therefore returns 0, which aligns with the interpretation that an empty numeric representation corresponds to a value of zero.
Strings that consist entirely of the digit 9 trigger the early-exit condition on the first iteration. The method detects the maximum digit immediately and returns 9 without scanning the rest of the string, demonstrating correct handling of the worst-case digit distribution.
Leading zeros are treated like any other digit; converting '0' to an integer yields 0, and the maximum calculation ignores them unless a larger digit appears later in the string.
Performance Optimizations
The algorithm achieves efficiency by performing a single linear pass over the input characters, maintaining only a constant-size integer variable. The most notable optimization is the early termination when a 9 is found. Because 9 is the theoretical upper bound for any digit, the loop can stop as soon as this value is observed, potentially reducing the number of iterations dramatically for inputs that contain a 9 early in the sequence.
The use of the built-in max function combined with direct integer conversion ensures that each iteration executes a minimal amount of work, avoiding additional data structures or auxiliary storage. This design results in low overhead and fast execution even for very long input strings.
ELI5 - Simplified Explanation
Imagine you have a long string of number cards lying on a table, and each card shows a digit between zero and nine. The goal is to figure out the biggest number you can see among all those cards.
The code starts by keeping a small box called result, which begins completely empty at zero. Then, it looks at every single card one by one, moving from the left side to the right side. For every card, the code checks if the number written on it is bigger than the number currently inside the box. If the new card has a larger number, the box gets updated to hold that new, higher value.
While looking at the cards, the code keeps a special eye out for the number nine. Because nine is the biggest digit possible, if the code ever finds a nine, it knows it does not need to look at any more cards. It saves time by stopping right there because nothing could ever be bigger than nine. Once all the cards are checked, or it finds a nine, the value held in the box is the answer.
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 | black-shadows | C++ | ||||||||
| chandansgowda | Python | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JS | |||||||||
| fpdjsns | C++ | |||||||||
| fsq | C++ | |||||||||
| halfrost | Go | |||||||||
| JoshCrozier | JS | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| suxrobGM | C# | |||||||||
| varunu28 | Java | |||||||||
| vthwang | Java | |||||||||
| withaarzoo | C++ | |||||||||
| YuriSpiridonov | Python | |||||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||||
| WalkCCC | C++, Python, Java | |||||||||
| leetcode.ca | C, C++, Go, Java, Python, Rust, TypeScript | |||||||||
| Progiez | C++, Java, Python | |||||||||
| black-shadows | C++, C++ | |||||||||
| chandansgowda | Python | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JavaScript | |||||||||
| fpdjsns | C++ | |||||||||
| fsq | C++ | |||||||||
| halfrost | Go | |||||||||
| JoshCrozier | JavaScript | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| suxrobGM | C# | |||||||||
| varunu28 | Java | |||||||||
| vthwang | Java | |||||||||
| withaarzoo | C++ | |||||||||
| YuriSpiridonov | Python |
Videos
Solved On The Same Day
- problem 784: Letter Case Permutation
- problem 2391: Minimum Amount of Time to Collect Garbage
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