-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathPartitioningIntoMinimumNumberOfDeci-BinaryNumbers.java
108 lines (73 loc) · 1.95 KB
/
PartitioningIntoMinimumNumberOfDeci-BinaryNumbers.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Source: https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/
4 Approaches
Basic Idea:
Just return the max digit
eg:
n = 1234 (take as many 1's as the value of digits i.e. take 1 1's for digit 1, take 3 1's for digit 3, take 4 1's for digit 4 and so on)
1111
+ 0111
+ 0011
+ 0001
----
1234
----
1st Approach
Time: O(n), where n is the length of the given string(n)
Space: O(1), in-place
*/
class Solution {
public int minPartitions(String n) {
int max = 0;
int len = n.length();
for(int i = 0; i < len; ++i) {
int digit = n.charAt(i) - '0';
if(digit > max) {
max = digit;
}
}
return max;
}
}
/*
2nd Approach (avoid charAt() repeatidly)
Time: O(n), where n is the length of the given string(n)
Space: O(n), char array is needed to access each character of the given string(s)
*/
class Solution {
public int minPartitions(String n) {
int max = 0;
for(char ch : n.toCharArray()) {
int digit = ch - '0';
if(digit > max) {
max = digit;
}
}
return max;
}
}
/*
3rd Approach (faster than Approach 2, by avoiding char('8') to digit(8) conversion repeatidly i.e. avoiding int digit = ch - '0')
Time: O(n), where n is the length of the given string(n)
Space: O(n), char array is needed to access each character of the given string(s)
*/
class Solution {
public int minPartitions(String n) {
int max = 48; // 48 is the ASCII value of 0(zero)
for(char ch : n.toCharArray()) {
if(ch > max) {
max = ch;
}
}
return max - 48; // subtract the 48(ASCII value of 0(zero)) from the max
// eg: if max digit in the string(n) is 8, then max will be 56(which is the ASCII value of 8) and 56 - 48 = 8 which will be desired output
}
}
/*
4th Approach(1 Liner)
*/
class Solution {
public int minPartitions(String n) {
return Character.getNumericValue(n.chars().max().getAsInt());
}
}