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, so you need to return a string instead of an integer.
Example 1:
Input: nums = [10,2] Output: "210"
Example 2:
Input: nums = [3,30,34,5,9] Output: "9534330"
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 109Problem Overview: You are given an array of non‑negative integers. Rearrange them so that their concatenation forms the largest possible number. The tricky part is that normal numeric sorting does not work. For example, 9 should come before 34, but 34 should come after 3 because 343 is smaller than 334. The solution depends on comparing numbers based on their concatenated order.
Approach 1: Custom Sorting Using Concatenation Comparison (O(n log n))
Convert all integers to strings and sort them using a custom comparator. For two values a and b, compare a + b with b + a. If a + b is larger, a should appear before b in the final ordering. This works because the goal is maximizing the combined string, not the numeric value of each element individually. The algorithm relies on a customized comparison inside a standard sorting routine.
After sorting, concatenate all strings to produce the final number. A special edge case appears when the largest value is "0". This means all numbers are zeros, so return "0" instead of something like "0000". The time complexity is O(n log n) due to sorting, and each comparison costs up to O(k) where k is the digit length. Space complexity is O(n) for storing string representations.
This approach combines ideas from array manipulation and greedy ordering. The greedy insight is that the locally optimal concatenation decision between two numbers leads to a globally optimal result after sorting.
Approach 2: Custom Comparator Using Heap (O(n log n))
Instead of sorting directly, push all numbers (as strings) into a heap that uses the same concatenation comparator rule. The heap ensures that every extraction returns the element that should appear next in the largest concatenated result. Each push and pop operation maintains the comparator order internally.
Repeatedly pop from the heap and append values to the result string. The comparison logic remains identical: choose the element where a + b produces a larger string than b + a. This method still performs about O(n log n) heap operations, and each comparison takes O(k). Space complexity remains O(n) for the heap structure and string storage.
Heap-based ordering is useful if elements are processed incrementally or when you prefer explicit priority queue control instead of full sorting. In most implementations, direct sorting is simpler and faster in practice.
Recommended for interviews: The custom sorting approach is what most interviewers expect. It demonstrates understanding of comparator logic and greedy ordering. Mentioning brute intuition (trying permutations) shows you understand the search space, but implementing the concatenation comparator with a + b vs b + a is the key insight that signals strong problem‑solving skills.
This approach involves converting each number to a string and sorting them based on a custom comparator. The key idea is to determine the order of two numbers a and b by comparing the concatenated results a+b and b+a as strings. Sorting the numbers in such a manner ensures that the concatenated string is the largest possible.
This solution first converts each number to a string and stores it in an array. It then sorts the array using a custom comparator. The comparator function forms two possible concatenated strings and orders based on which string is larger. After sorting, it concatenates the sorted numbers to form the largest number and checks for leading zeros.
Time Complexity: O(n log n), where n is the number of elements, due to sorting. Comparison operation within the sort is O(1) as it's based on string concatenation and comparison.
Space Complexity: O(n), where n is the number of integers, due to the storage of string representations of integers.
This approach involves using a custom comparator with a min-heap to continuously extract the largest possible number combination. This can be more efficient in some cases where we require efficient access to the largest current element for concatenation.
The solution uses a max-heap implemented with negative values in Python’s built-in heapq, to facilitate priority retrieval of the ‘largest’ lexical integer string. The heap ensures the largest value is popped and concatenated first.
Python
Time Complexity: O(n log n), dominated by the heap operations.
Space Complexity: O(n) due to the heap storage.
| Approach | Complexity |
|---|---|
| Custom Sorting Using Comparison of Concatenation | Time Complexity: O(n log n), where n is the number of elements, due to sorting. Comparison operation within the sort is O(1) as it's based on string concatenation and comparison. |
| Custom Comparator Using Heap | Time Complexity: O(n log n), dominated by the heap operations. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Custom Sorting with Concatenation Comparator | O(n log n * k) | O(n) | General solution; simplest and most common interview implementation |
| Heap with Custom Comparator | O(n log n * k) | O(n) | When elements arrive incrementally or priority queue ordering is preferred |
Largest Number - Leetcode 179 - Python • NeetCode • 82,291 views views
Watch 9 more video solutions →Practice Largest Number with our built-in code editor and test cases.
Practice on FleetCode