Arrange Numbers to Form the Largest Possible Value
Implement largestArrangement
Given a list of non-negative integers, arrange them so that when written one after another they form the largest possible number, and return that number as a string. If every integer is 0, return "0".
Trying every ordering is factorial. Sorting the numbers by size is wrong (9 should come before 91, and 91 before 90). The right greedy rule compares two numbers by the strings they form when joined in each order: a should come before b when "a" followed by "b" is larger than "b" followed by "a".
Example 1:
Input: nums = [8,89,9,91,90]
Output: "99190898"
Example 2:
Input: nums = [0,0,0]
Output: "0"
Example 3:
Input: nums = [12,121]
Output: "12121"
+ 9 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 6 and 0 ≤ nums[i] ≤ 999 - ●
Arrange all the numbers in some order and write them one after another (no separators) to form a single non-negative number - ●
Return the largest number that can be formed, as a string — the result can be far too large for an ordinary integer - ●
If every number is 0 the result is the single character "0", never something like "000"
nums =
[8, 89, 9, 91, 90]