κ°€μž₯ 큰 수 - leetcode 179번

문제

179. Largest Number

풀이

class Solution:
    @staticmethod
    def swap(n1: int, n2: int) -> bool:
        return str(n1) + str(n2) < str(n2) + str(n1)

    def solution(self, nums):
        i = 1
        while i < len(nums):
            j = i
            while j > 0 and self.swap(nums[j - 1], nums[j]):
                nums[j - 1], nums[j] = nums[j], nums[j - 1]
                j -= 1
                # 1. μ™Όμͺ½μ—μ„œλΆ€ν„° 인덱슀 1λΆ€ν„° μ‹œμž‘ν•΄μ„œ μ •λ ¬ν•΄λ‚˜κ°
                # 2. μ™Όμͺ½μ€ λ‹€ μ •λ ¬ λ˜μ–΄μžˆκΈ° λ•Œλ¬Έμ— λŒ€μ†Œ λΉ„κ΅μ‹œ ν•œλ²ˆλ§Œμ— λλ‚ μˆ˜λ„ μžˆμ–΄μ„œ λ§Œμ•½ μ „λΆ€ μ •λ ¬λ˜μ–΄μžˆμœΌλ©΄ O(n)도 κ°€λŠ₯
            i += 1
        return str(int(''.join(map(str, nums))))
        # 3. mapν•¨μˆ˜ μ‚¬μš©λ²• μ°Έκ³ 

s = [3, 30, 34, 5, 9]
result = Solution().solution(s)
print(result)