There is well known algorithmic problem, given array of numbers e.g. [1, 20, 3, 14]
arrange numbers in such a way that they form biggest number possible, in this case 320141
.
There is plenty of solutions on SO and other sites, using the following algorithm:
String[] strs = new String[nums.length];
for(int i=0; i<nums.length; i++){
strs[i] = String.valueOf(nums[i]);
}
Arrays.sort(strs, new Comparator<String>(){
public int compare(String s1, String s2){
String leftRight = s1+s2;
String rightLeft = s2+s1;
return -leftRight.compareTo(rightLeft);
}
});
StringBuilder sb = new StringBuilder();
for(String s: strs){
sb.append(s);
}
return sb.toString();
It certainly works, but I cannot find a formal proof of this algorithm. There is one answer on quora, but I wouldn't call it a formal proof.
Does anyone can give me a sketch of proof or reference some book or article? How one can get on this solution from the original problem?
PS I tried to solve original problem but my solution was wrong, I couldn't get it right, and now I could not fully understand solution.