Partitioning Into Minimum Number Of Deci-Binary Numbers — LeetCode 1689 Python Solution

MediumGreedyString
Problem
#1689
Pattern
Greedy
Reading time
2 min

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

Python
class Solution:
    def minPartitions(self, n: str) -> int:
        return int(max(n))

Complexity

MeasureComplexity
TimeO(n), where n is the length of the string
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview