-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_1689.java
32 lines (31 loc) · 1.09 KB
/
prob_1689.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
* Medium
* 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 minimum number of positive deci-binary numbers needed so that they sum up to n.
*/
public class prob_1689 {
public static void main(String[] args) {
Solution_1689 solution = new Solution_1689();
String n = "32";
System.out.println(solution.minPartitions(n));
}
}
/**
* Solution using string integer parsing
* Time Complexity - O(n), Space Complexity - O(1)
* Observation - Any deci-binary number can be constructed using the largest digit in the string
* Because that is the largest # of ones required
*/
class Solution_1689 {
public int minPartitions(String n) {
int max = 1;
for (char c : n.toCharArray()) {
max = Math.max(max, c-'0');
if(max == 9) break;
}
return max;
}
}