LeetCode solutions

2544. Minimum Addition to Make Integer Beautiful

My accepted Python solution to LeetCode problem 2544, Minimum Addition to Make Integer Beautiful, running in 3ms.

  • Difficulty: Medium
  • Python
  • Runtime 3ms
  • Memory 17.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.2MB, accepted 2025-12-31.

python
class Solution:
    def makeIntegerBeautiful(self, n: int, target: int) -> int:
        def digit_sum(x):
            return sum(int(d) for d in str(x))
        
        original = n
        place = 1
        
        while digit_sum(n) > target:
            # Round up to next multiple of 10^place
            # This sets the last 'place' digits to 0
            n = (n // (10 * place) + 1) * (10 * place)
            place *= 10
        
        return n - original

Source