https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/

 

class Solution:
    def makeIntegerBeautiful(self, n: int, target: int) -> int:
        def isBeautiful(num):
            dsum = 0
            while num:
                dsum += num % 10
                num //= 10
            return dsum <= target
        
        x = 0
        e = 0
        
        while not isBeautiful(n + x):
            ethDigitFromRight = ((n + x) // (10 ** e)) % 10
            if ethDigitFromRight != 0:
                x += (10**e) * (10 - ethDigitFromRight)
            e += 1
        
        return x

뷰티풀하지 않다면 숫자를 더해서 각 자릿수를 0으로 만들어보자.

+ Recent posts