1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
</> Solution
/**
* Problem: LeetCode 1689 - Partitioning Into Minimum Number Of Deci-Binary Numbers
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int minPartitions(String n) {
int maxDigit = 0;
int len = n.length();
for (int i = 0; i < len; i++) {
int digit = n.charAt(i) - '0';
if (digit > maxDigit) {
maxDigit = digit;
if (maxDigit == 9) {
return 9; // Early exit since no decimal digit can exceed 9
}
}
}
return maxDigit;
}
}
"""
Problem: LeetCode 1689 - Partitioning Into Minimum Number Of Deci-Binary Numbers
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def minPartitions(self, n: str) -> int:
for d in "987654321":
if d in n:
return int(d)
return 0
/**
* Problem: LeetCode 1689 - Partitioning Into Minimum Number Of Deci-Binary Numbers
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
int minPartitions(string n) {
int maxDigit = 0;
for (char ch : n) {
int digit = ch - '0';
if (digit > maxDigit) {
maxDigit = digit;
if (maxDigit == 9) {
return 9; // Early exit optimization
}
}
}
return maxDigit;
}
};
/**
* Problem: LeetCode 1689 - Partitioning Into Minimum Number Of Deci-Binary Numbers
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {string} n
* @return {number}
*/
var minPartitions = function(n) {
let maxDigit = 0;
for (let i = 0; i < n.length; i++) {
const digit = n.charCodeAt(i) - 48;
if (digit > maxDigit) {
maxDigit = digit;
if (maxDigit === 9) {
return 9;
}
}
}
return maxDigit;
};