Published:
Updated:


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