Largest number
September 3, 2022
array-and-hashmapProblem URL: Largest number
We will take the input array, and sort it with our custom compare method, where we sort it in natural string order in reverse. Then we convert the the numbers into string and merge it together to get our result. But first we will check whether the numbers input only has 0 in it, if yes we then return 0.
class Solution:
def largestNumber(self, nums: List[int]) -> str:
if set(nums) == {0}:
return "0"
def compare(a, b):
return int(str(b)+str(a)) - int(str(a)+str(b))
nums = sorted(nums, key=cmp_to_key(compare))
return "".join([str(n) for n in nums])
Time Complexity: O(nlog(n))
Space Complexity: O(1)