折腾了半天,还是要找对思路
题目大意
给定任意一个数字 m,然后给出数字 n,则需在 m 中去掉 n 位数,保持各位顺序不变的情况下,得到最大数。
输入描述
输入整数n,m (1<=n<=1e100,1<=m<=100)
输出描述
输出删除后的最大数。
示例
输入:1234 2
输出:34
解题思路
计算出结果坑位数(size = n.length - m)
每一个坑位数的最大值下标[0, size]、[0, size + 1]、[0, size + 2]……
最难理解在第二点,因为题目说保障前后顺序,所以比如 12345 2,那么,坑位数为 3,第一个位置的最大值在[0, 2],第二个[0, 3],第三个[0, 4],中途如果被用过的数字需要做下标记,后面的坑位不能再使用
代码
class Solution:
def __init__(self) -> None:
pass
def solution(self, n):
result = None
# TODO: 请在此编写代码
num = n[0]
num_list = []
while num > 0:
num_list.insert(0,num % 10)
num = num // 10
if len(num_list) <= n[1]:
result = 0
return result
mm = len(num_list) - n[1]
result_list = []
index = 0
for j in range(mm):
num_list_temp = num_list[index:(n[1]+j+1)]
maxvalue = max(num_list_temp)
for jj in range(index,(n[1]+j+1)):
if num_list[jj] == maxvalue:
index = jj
num_list[index] = -1
break
result_list.append(maxvalue)
result = 0
for jj in range(len(result_list)):
result += result_list[jj]*(10**(len(result_list)-jj-1))
return result
if __name__ == "__main__":
n = [int(item) for item in input().strip().split()]
sol = Solution()
result = sol.solution(n)
print(result)