# 2457. Minimum Addition to Make Integer Beautiful ###### tags: `Leetcode` `Medium` Link: https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/description/ ## 思路 $O(logNlogN)$ $O(1)$ 对于n=123456来说 如果现在n的digit sum大于target 下一个比n大并且digit sum小于等于target的就是123460 然后是123500 也就是```123456 -> 123460 -> 123500 -> 124000 -> 130000 -> 2000000``` 所以如果n的digit sum大于target 我们就令```n = n//10+1``` ## Code ```java= class Solution { public long makeIntegerBeautiful(long n, int target) { long n0 = n, base = 1; while(sum(n)>target){ n = n/10+1; base *= 10; } return n*base-n0; } private int sum(long n){ int res = 0; while(n>0){ res += n%10; n /= 10; } return res; } } ```