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
๐ค Output