Ctrl K

Biggest Number From Pieces

Medium
You are given a list of non-negative integers pieces. Write their decimal forms one after another, with no separators, in some order, so that they form one long number. Each piece is used exactly once. Choose the order that makes the resulting number as large as possible and return it as a string (it may not fit in any integer type). The answer must have no leading zeros: if the number is zero, return "0".
Example 1
pieces=[5, 51, 56] → '56551'

56551 beats 55651 and 51556, so the order is 56, then 5, then 51.

Example 2
pieces=[0, 0, 0] → '0'

Gluing only zeros gives the number 0; no leading zeros are written.

Example 3
pieces=[12, 121] → '12121'

12121 is larger than 12112, so 12 goes first.

Constraints
  • 1 ≤ len(pieces) ≤ 100
  • 0 ≤ pieces[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRound Prices to a Total MediumPalindromes From Shared Letters MediumDistinct Word Shorthands HardAnagram Buckets MediumStudios for Recording Sessions Medium
loading Python…
Input
pieces=[5, 51, 56]
Expected
'56551'

Run checks the first 3 tests in your browser. Submit checks all 151 tests on the server, hidden ones included.