# 贪心算法-移除k个数让数串尽可能小

```python
'''
已知一个使用字符串表示的非负整数num，将num中的k个数字移除，求移除k个数字后，可以获得的最小的可能的新数字。
输入 : num = “1432219” , k = 3 在去掉3个数字后得到的很多很多可能里，如1432、4322、2219、1219、1229...;
去掉数字4、3、2得到的1219最小
'''

def new_num(str_num, k):
    stack_num = []
    stack_num.append(str_num[0])
    for i in range(1, len(str_num)):
        # 首字母 不能0
        if len(stack_num) > 0 and int(str_num[i]) < int(stack_num[-1]) and k > 0:
            stack_num.pop()
            k -= 1

        if len(stack_num) > 0 or str_num[i] != '0':
            stack_num.append(str_num[i])

    str_num = ''.join(stack_num)

    return str_num


if __name__ == '__main__':
    s = new_num('1432219', 3)
    print(s)
    s = new_num('100200', 1)
    print(s)
'''
1219
200
'''
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/si-xiang-lei/tan-xin/yi-chu-k-ge-zi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
