MediumArraysSortingGreedyStrings

1 approach, code in every language

Problem description

Given a collection of non-negative integers, arrange them so that gluing their decimal representations together produces the largest possible number. Return the result as a string, because it can be far too long to fit in a normal integer.

Watch out for the all-zero case: if every value is zero the answer should be a single "0" rather than a run of zeros.

Example 1

Input: nums = [3,30,34,5,9]

Output: "9534330"

Ordering the pieces as 9, 5, 34, 3, 30 yields the biggest concatenation.

Example 2

Input: nums = [10,2]

Output: "210"

Constraints

  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 10^9

After the pattern

Now solve one you haven't seen.

Greatest Shipment Tag is our own problem on the technique behind Largest Number: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free