Show problems as
Classic problem #179MediumArrayStringGreedySorting

Largest Number

Real-world version: Greatest Shipment Tag
Tracked in this browser only
Read Largest Number on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstSorting · Arrays, animated step by step

A packing station prints the part number of every item in a shipment on its own slip, then glues the slips into one row that reads as a single numeral: the shipment tag. The station picks the order of the slips, and each slip is used exactly once with its digits kept as printed.

Given the part numbers parts, return the greatest tag the station can glue, as a string. A tag is never printed with a leading zero, so when every part number is 0 the tag reads "0". The answer comes back as a string because a tag can run far past what a machine integer holds.

Examples

Example 1

Input
parts = [7, 70]
Output
"770"

Gluing 7 in front gives 770, while 70 in front gives 707, so 7 leads.

Example 2

Input
parts = [2, 25, 251]
Output
"252512"

The order 25, 251, 2 reads as 252512, which beats every other arrangement, including 251 first at 251252.

Example 3

Input
parts = [2, 10, 0]
Output
"2100"

2 leads, then 10, then 0; putting 0 anywhere earlier only shrinks the tag.

Constraints

  • 1 <= parts.length <= 100
  • 0 <= parts[i] <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def greatest_tag(parts: list[int]) -> str:
Java
public String greatestTag(int[] parts)