Created
March 1, 2026 17:27
-
-
Save tatsuyax25/9fc0c4a1c81847b671dd0d2b3a6c6c64 to your computer and use it in GitHub Desktop.
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. Given a string n that represents a positive decimal integer, return the m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {string} n | |
| * @return {number} | |
| */ | |
| var minPartitions = function(n) { | |
| // We want the minimum number of deci-binary numbers needed. | |
| // A deci-binary number can only contribute 0 or 1 to each digit. | |
| // Therefore, the number of deci-binary numbers required is equal | |
| // to the maximum digit in the string. | |
| let maxDigit = 0; // Track the largest digit we encounter | |
| // Loop through each character in the string | |
| for (let char of n) { | |
| const digit = char - '0'; // Convert character to number | |
| // Update maxDigit if this digit is larger | |
| if (digit > maxDigit) { | |
| maxDigit = digit; | |
| } | |
| // Optimization: if we ever hit 9, that's the maximum possible | |
| if (maxDigit === 9) { | |
| return 9; | |
| } | |
| } | |
| return maxDigit; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment