Skip to content

Commit 8e00b6e

Browse files
authored
Create minimum-addition-to-make-integer-beautiful.py
1 parent 75f2bd0 commit 8e00b6e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
# greedy
5+
class Solution(object):
6+
def makeIntegerBeautiful(self, n, target):
7+
"""
8+
:type n: int
9+
:type target: int
10+
:rtype: int
11+
"""
12+
total, m = 0, n
13+
while m:
14+
total += m%10
15+
m //= 10
16+
m, l = n, 0
17+
while total > target:
18+
while True:
19+
total -= m%10
20+
m //= 10
21+
l += 1
22+
if m%10 != 9:
23+
break
24+
total += 1
25+
m += 1
26+
return m*10**l-n

0 commit comments

Comments
 (0)