文章のままを書けばいいと思う。
abc192c.py
N,K = map(int,input().split())
for _ in range(K):#O(10^5)
num0 = sorted(list(str(N)))
num1 = sorted(num0,reverse=True)
g1 = int("".join(num1))
g2 = int("".join(num0))
N = g1-g2
print(N)#192ms
スピードをあげるために def は must.
def の中に def を埋め込んでみた。
abc192c.py
def solv():
N,K = map(int,input().split())
def helper(n):
num = "".join(sorted(list(str(n))))
return int(num[::-1])-int(num)
for _ in range(K):
N = helper(N)
print(N)
solv()#132ms
60ms 改善できた。30ms 代の回答を読んだが
全く訳が分からん。また時間を置いてチャレンジしよう。