Largest Number - Problem
Imagine you have a collection of non-negative integers and you want to arrange them to form the largest possible number. This isn't as simple as sorting them in descending order! For example, given [3, 30, 34, 5, 9], the largest number isn't "93453430" but actually "9534330". The trick is determining which number should come first when two numbers could be concatenated in different orders. Your task is to arrange the numbers optimally and return the result as a string (since the number might be too large for standard integer types).

Input & Output

example_1.py โ€” Standard Case
$ Input: [10, 2]
โ€บ Output: "210"
๐Ÿ’ก Note: Comparing concatenations: '10'+'2'='102' vs '2'+'10'='210'. Since '210' > '102', we put '2' first, giving us '210'.
example_2.py โ€” Multiple Digits
$ Input: [3, 30, 34, 5, 9]
โ€บ Output: "9534330"
๐Ÿ’ก Note: Using our custom comparator: '9' comes first (largest), then '5' (5+34=534 > 34+5=345), then '34' (34+3=343 > 3+34=334), then '3' (3+30=330 > 30+3=303), finally '30'.
example_3.py โ€” All Zeros
$ Input: [0, 0, 0]
โ€บ Output: "0"
๐Ÿ’ก Note: Edge case: when all numbers are 0, the result should be '0' (not '000'). We handle this by checking if the first character of our result is '0'.

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 0 โ‰ค nums[i] โ‰ค 109
  • The result may be very large, so return it as a string
  • All numbers are non-negative integers

Visualization

Tap to expand
The Custom Comparator Strategy330Which should come first?Option 1: Put 3 firstConcatenation: '3' + '30' = '330'Result preview: 330...Option 2: Put 30 firstConcatenation: '30' + '3' = '303'Result preview: 303...Decision: Since '330' > '303', we choose Option 1 (3 before 30)Complete Example: [3, 30, 34, 5, 9]Original[3, 30, 34, 5, 9]Naive sort:[9, 5, 34, 30, 3]โ†’ "953430"โ†’Custom Sorted[9, 5, 34, 3, 30]Using xy > yx rule:9โ†’5โ†’34โ†’3โ†’30โ†’ "9534330" โœ“๐Ÿ”‘ The Magic Formula:For any two numbers A and B: A comes before B if (A + B as string) > (B + A as string)This greedy choice always leads to the globally optimal solution!Time: O(n log n) | Space: O(n) | The optimal solution! ๐Ÿš€
Understanding the Visualization
1
Convert to Comparable Format
Transform all numbers to strings so we can easily concatenate and compare them
2
Define Team Chemistry
For any two numbers x and y, determine their order by checking if xy produces a larger number than yx
3
Sort Using Team Chemistry
Apply this comparison rule to sort all numbers, ensuring optimal positioning
4
Assemble the Final Team
Concatenate all sorted numbers to form the largest possible result
Key Takeaway
๐ŸŽฏ Key Insight: The custom comparator xy > yx ensures that each local decision (which of two numbers comes first) contributes to the globally optimal solution, eliminating the need to check all permutations.
Asked in
Google 45 Amazon 38 Meta 22 Microsoft 31
89.0K Views
Medium-High Frequency
~25 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen