Solution
from typing import List
class Solution:
# μ½μ
μ λ ¬ λ¬Έμ
# κ°μ₯ ν° μλ₯Ό λ§λ€μ΄μ λ¬Έμμ΄λ‘ λ°ν
def largestNumber(self, nums: List[int]) -> str:
# μμλ¦¬κ° κ°μ₯ ν° μλ‘ μ λ ¬, κ·Έ λ€μμλ κ·Έ λ€μ μ«μλ€λ‘ μ λ ¬
# μ½μ
μ λ ¬λ μ€μ μ΄μ©νλ©΄ λ λ―?
for i in range(len(nums)):
# Discusstion:
# The solution of the problem rests on observing that if AB > BA.
# Then we have ACB > BCA for any C.
for j in range(i, 0, -1):
if int(str(nums[j - 1]) + str(nums[j])) < int(str(nums[j]) + str(nums[j - 1])):
nums[j - 1], nums[j] = nums[j], nums[j - 1]
continue
break
return str(int(''.join(list(map(str, nums)))))
Reference
Leave a comment