Skip to content
BytePatterns

Largest Number Arrangement

MediumSorting#custom-comparator#sorting~30m

Problem

Given non-negative whole numbers, glue them together in some order so the resulting number is as large as possible, and return it as text. Each number is used exactly once and its own digits are never rearranged. A result made only of zeros must read as a single zero.

Examples

Input:  nums = [3, 30, 34, 5, 9]
Output: "9534330"
Why:    34 must come before 3, which must come before 30
Input:  nums = [10, 2]
Output: "210"
Why:    the larger number is not the one that belongs first
Input:  nums = [0, 0]
Output: "0"
Why:    edge case, gluing zeros must not produce a padded result

Hints

0 / 3

Stuck on the idea rather than the code? Which Sort When? covers it.