MediumArraysSortingGreedyStrings

Largest Number

LeetCode
1 approach, code in all languages

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
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now