MEDIUM Greedy / String

179. Largest Number

๐Ÿ“– Problem

Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since the result may be very large, you need to return a string instead of an integer.

๐Ÿง  Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • โ€ขArray methods (`push`, `pop`, `shift`, `slice`)
  • โ€ขObject/Map/Set usage patterns
  • โ€ขFunction parameter and return typing

Logical Thinking Concepts

  • โ€ขDefine invariants before coding
  • โ€ขCheck edge cases first (`[]`, single element, duplicates)
  • โ€ขEstimate time/space before implementation
  • โ€ขApply Greedy reasoning pattern

๐Ÿ’ก Approach

  • โ†’ Convert numbers to strings and sort them
  • โ†’ Custom sort: compare two strings by checking different lengths
  • โ†’ Time: O(n log n), Space: O(n)

๐Ÿ› ๏ธ Hints & Pitfalls

Hints

  • โ€ขConvert numbers to strings and sort them
  • โ€ขCustom sort: compare two strings by checking different lengths
  • โ€ขTime: O(n log n), Space: O(n)

๐Ÿงช Test Cases

Hidden tests on submit: 1

Test Case 1
Not run
Input:
largestNumber([10,2]);
Expected:
"210"
Test Case 2
Not run
Input:
largestNumber([3,30,34,5,9]);
Expected:
"9534330"
Test Case 3
Not run
Input:
largestNumber([0]);
Expected:
"0"

๐Ÿ“ Code Editor

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit