Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunjun committed Sep 25, 2020
1 parent 8eeed54 commit 78f4a0a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion python/problem-math/largest_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# https://leetcode.com/problems/largest-number/solution


from functools import cmp_to_key
from typing import List


Expand Down Expand Up @@ -52,7 +53,7 @@ def largestNumber1(self, nums):
# https://leetcode.com/explore/challenge/card/september-leetcoding-challenge/557/week-4-september-22nd-september-28th/3472
# runtime; 36ms, 84.93%
# memory; 14.1MB
def largestNumber(self, nums: List[int]) -> str:
def largestNumber2(self, nums: List[int]) -> str:
maxLen = max(len(str(n)) for n in nums)
sortedNums = sorted([(n, int(str(n) + str(n)[-1] * (maxLen - len(str(n))))) for n in nums], key=lambda t: -t[1])
for i, (n, modified) in enumerate(sortedNums):
Expand All @@ -63,6 +64,26 @@ def largestNumber(self, nums: List[int]) -> str:
sortedNums[i - 1], sortedNums[i] = sortedNums[i], sortedNums[i - 1]
return str(int(''.join(str(n) for n, _ in sortedNums)))

# runtime; 44ms, 52.70%
# memory; 14.1MB, 5.29%
def largestNumber(self, nums: List[int]) -> str:

def comparator(a, b):
strA, strB = str(a), str(b)
firstA, firstB = int(strA[0]), int(strB[0])
if firstA > firstB:
return -1
if firstA < firstB:
return 1
numAB, numBA = int(strA + strB), int(strB + strA)
if numAB > numBA:
return -1
if numAB < numBA:
return 1
return 0

return str(int(''.join([str(n) for n in sorted(nums, key=cmp_to_key(comparator))])))


s = Solution()
data = [([10, 2], '210'),
Expand Down

0 comments on commit 78f4a0a

Please sign in to comment.