Partitioning Into Minimum Number Of Deci-Binary Numbers — LeetCode 1689 Python Solution
- Problem
- #1689
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
The problem
A decimal number is called deci-binary if each of its digits is either 0 or 1 without any leading zeros. For example, 101 and 1100 are deci-binary, while 112 and 3001 are not.
Example
- Input
- n = "32"
- Output
- 3
- Explanation
- 10 + 11 + 11 = 32
Python solution
class Solution:
def minPartitions(self, n: str) -> int:
return int(max(n))Complexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the string |
| Space | O(1) auxiliary |
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers?
- LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers?
- The Python solution on this page runs in O(n), where n is the length of the string.
- What is the space complexity of LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers cover?
- LeetCode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers is tagged Greedy and String on LeetCode.