MediumNo. 1689

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

Pattern
Greedy
Topics
Greedy, String
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

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

Complexity

Time
O(n)where n is the length of the string
Space · auxiliary
O(1)

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

Related problems
No.ProblemDifficulty
0179Largest NumberMedium
0316Remove Duplicate LettersMedium
0402Remove K DigitsMedium
0420Strong Password CheckerHard
0649Dota2 SenateMedium
0680Valid Palindrome IIEasy

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.

Free to download. Check it runs on your machine, then pick a plan.