Write a function solution that given a three digit integer N and Integer K, returns the maximum possible three digit value that can be obtained by performing at most K increases by 1 of any digit in N
given N=192, N=4, output should be 591
def maxNumber(n:int, k:int) ->int:
i = 0
while k > 0 and i < 3:
n_str = str(n)
tmp = int(n_str[i])
if tmp == 9:
i += 1
else:
new_n_str = n_str[:i] + str(tmp+1) + n_str[i+1:]
if int(new_n_str) > n:
n = int(new_n_str)
k -= 1
return n
n = 191
k = 4
print(maxNumber(n,k))
Comments
Leave a comment