All problems
0045MediumArrayStringGreedySorting

Greatest Shipment Tag

Tracked in this browser only
Write code

Trains the technique from

LeetCode 179Largest Number

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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