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. Since the result can be extremely large, it must be returned as a string rather than a numeric value.
Approach 1: Custom Sorting Using Concatenation Comparison (Greedy + Sorting) (Time: O(n log n * k), Space: O(n))
The key insight is that normal numeric sorting does not work. Instead, compare two numbers a and b by checking which concatenation produces a larger string: ab or ba. If ab > ba, then a should appear before b in the final order. This turns the problem into a custom comparator for sorting.
Convert all numbers to strings so concatenation is easy. Then sort the array using the comparator that compares a+b with b+a. After sorting in descending order of this rule, join all strings to form the final result. A small edge case appears when the largest element becomes "0" after sorting; this means every number is zero, so the correct output is simply "0" instead of multiple zeros.
This greedy ordering works because every local comparison ensures the globally optimal prefix for the final number. The algorithm is dominated by sorting, which performs O(n log n) comparisons, and each comparison takes up to O(k) time where k is the average digit length.
Approach 2: Custom Comparator Using Heap (Greedy + Heap) (Time: O(n log n * k), Space: O(n))
Another way to enforce the same ordering rule is to push elements into a priority queue (max‑heap) that uses the same concatenation comparison logic. Instead of sorting the entire array at once, repeatedly extract the element that should appear next in the final number.
Insert all numbers (as strings) into a heap where the comparator prioritizes the string that forms the larger concatenated value when paired with others. Each pop operation gives the next best candidate for the final string. Append popped elements sequentially to build the result.
This approach still relies on the same greedy principle but replaces sorting with a heap from array elements. The complexity remains O(n log n * k) because each insertion and removal from the heap costs O(log n), and the comparator still performs string concatenation comparisons.
Recommended for interviews: The custom sorting approach is the standard and expected solution. It demonstrates understanding of greedy ordering and custom comparators. Interviewers usually want to see the insight that comparing ab vs ba determines the correct order. The heap version works but is rarely necessary unless the problem specifically requires incremental extraction.
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.
C++
Java
Python
C#
JavaScript
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.
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) | Best general solution; standard interview approach using greedy ordering and custom sort |
| Heap with Custom Comparator | O(n log n * k) | O(n) | Useful when elements must be processed incrementally or when using priority queues |
8 patterns to solve 80% Leetcode problems • Sahil & Sarra • 656,554 views views
Watch 9 more video solutions →Practice Largest Number with our built-in code editor and test cases.
Practice on FleetCode