[GeeksForGeeks] Find Maximum Number that can be formed using given all digits in a given integer.
Find Maximum Number that can be formed using all digits in the given integer.
Solution 1. O(n * log n) runtime, O(n) space using sorting
A simple solution is to create an array of size n, n is the total number of digits.
1. Fill in the array in O(n) time.
2. Sort the array in descending order in O(n * log n) time.
3. construct a new number in O(n) time.
Solution 2. O(n) runtime, O(1) space
Since we know all digits are in between 0 and 9, we can use O(1) space to get a mapping
from each digit in [0, 9] and its appearance number in the given integer.
Then in O(n) time, we reconstruct the max number by traversing the mapping from 9 to 0.
1 public class MaxNum { 2 public int getMaxNum(int val) { 3 int[] map = new int[10]; 4 int modulous = 0; int max = 0; 5 while(val != 0) { 6 modulous = val % 10; 7 map[modulous]++; 8 val /= 10; 9 } 10 for(int i = 9; i >= 0; i--) { 11 while(map[i] != 0) { 12 max = max * 10 + i; 13 map[i]--; 14 } 15 } 16 return max; 17 } 18 }
Follow up question: Instead of given an integer, you are now given some strings, each string contains some number of digits.
What is the max number you can get out of these strings? You are allowed to combine the strings in different order, but you can't
change each individual string.
The key of this problem is to sort the input strings in a way that we can reconstruct the string with the max value by a linear scan.
For strings that have the same length, we follow strings' natural ordering. In the case of different lengths' strings, the following
orders are used.
We pad the shorter string using its last character to be the same length of the longer string.
For example,
to compare "33" and "332", we compare "333" and "332", so "33" is bigger than "332";
to compare "33" and "333", we compare "333" and "333", so "33" is equal to "333";
to compare "33" and "334", we compare "333" and "334", so "33" is smaller than "334";
1 public String getMaxNumStr(String[] strs) { 2 Comparator<String> comp = new Comparator<String>() { 3 public int compare(String s1, String s2) { 4 int idx1 = 0, idx2 = 0; 5 while(idx1 < s1.length() && idx2 < s2.length()) { 6 if(s1.charAt(idx1) < s2.charAt(idx2)) { 7 return 1; 8 } 9 else if(s1.charAt(idx1) > s2.charAt(idx2)) { 10 return -1; 11 } 12 idx1++; 13 idx2++; 14 } 15 if(idx1 == s1.length() && idx2 == s2.length()) { 16 return 0; 17 } 18 else if(idx1 < s1.length()) { 19 while(idx1 < s1.length()) { 20 if(s1.charAt(idx1) < s2.charAt(s2.length() - 1)) { 21 return 1; 22 } 23 else if(s1.charAt(idx1) > s2.charAt(s2.length() - 1)) { 24 return -1; 25 } 26 idx1++; 27 } 28 } 29 else { 30 while(idx2 < s2.length()) { 31 if(s1.charAt(s1.length() - 1) < s2.charAt(idx2)) { 32 return 1; 33 } 34 else if(s1.charAt(s1.length() - 1) > s2.charAt(idx2)) { 35 return -1; 36 } 37 idx2++; 38 } 39 } 40 return 0; 41 } 42 43 }; 44 Arrays.sort(strs, comp); 45 StringBuilder sb = new StringBuilder(); 46 for(int i = 0; i < strs.length; i++) { 47 sb.append(strs[i]); 48 } 49 return sb.toString(); 50 }