Arrange for the biggest number
Glue numbers together in the order that makes the biggest result, by inventing your own rule for which comes first. About 14 minutes.
A rule between two items
Given 3, 30 and 34, which order makes the biggest number when glued together? Putting the biggest number first gives 34, 30, 3, which makes 34303. But the order 34, 3, 30 makes 34330, which is bigger.
Sorting by the numbers themselves doesn't work. Instead decide for each pair which should come first: put a before b if gluing a then b gives a bigger result than b then a. If the rule is consistent, sorting with it gives the best arrangement.
a, b = "3", "30" first = a + b # "330" second = b + a # "303"
Try the pair 8 and 89: 889 versus 898. 898 is bigger, so 89 goes before 8.
Should 9 come before 91 when making the biggest number?
- AYes, because 991 is bigger than 919
- BNo, because 91 is the bigger number
- CIt makes no difference
Show the answer
Yes, because 991 is bigger than 919. Compare the glued results: 9 then 91 is 991, and 91 then 9 is 919.
Biggest number from the pieces
nums is a list of non-negative whole numbers. Arrange them in some order and glue their digits together to make the biggest possible number. Return that number as a string, because it may be very long. If the list is all zeros, return "0".
nums = [3, 30, 34, 5, 9] → "9534330"
1 ≤ length ≤ 300 · 0 ≤ value ≤ 1,000,000,000