#C2920. Remove K Digits
Remove K Digits
Remove K Digits
You are given a non-negative integer num represented as a string and an integer k. Your task is to remove exactly k digits from the number so that the new number is the smallest possible. The answer should not contain any leading zeros (except for the number 0 itself).
For example, if num = "1432219"
and k = 3
, removing the three digits 4
, 3
and 2
yields 1219
, which is the smallest possible result.
The process can be mathematically interpreted as follows:
Given the number as a string \( s = s_1s_2\ldots s_n \), remove k digits to form a new sequence \( s' \) such that \( s' \) is minimal in numerical value. The optimal strategy is to remove digits that are greater than their subsequent digits.
You need to read the input from standard input and write the result to standard output.
inputFormat
The input consists of two lines:
- The first line contains a non-negative integer
num
represented as a string. - The second line contains an integer
k
, which is the number of digits to remove (\( 0 \leq k \leq |num| \)).
You can assume that num
does not have any leading zeros, except in the case where the number itself is 0.
outputFormat
Output a single line containing the smallest possible number (as a string) after removing exactly k
digits. If the result is empty, output 0
.
1432219
3
1219
</p>