#K13336. Remove K Digits
Remove K Digits
Remove K Digits
You are given a string num representing a non-negative integer and an integer k. Your task is to remove at most k digits from num such that the resulting number is the smallest possible.
The challenge is to carefully choose which digits to remove while maintaining the order of the remaining digits. When removing a digit, it might be beneficial to remove a larger digit that appears before a smaller digit in order to achieve a lower overall value.
The answer should be given in standard form (as a string without leading zeros) except if this results in an empty string, in which case output "0".
Mathematically, if you remove the digits at indices \(i_1, i_2, \dots, i_m\) from the number (with \(m \le k\)), the remaining sequence forms the new number which should be minimized.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a string
num
which represents the original number. - The second line contains an integer
k
, the maximum number of digits that can be removed.
outputFormat
Output a single line (stdout) containing the smallest possible number (as a string) after removing at most k
digits. Leading zeros should be removed unless the number is 0.
1432219
3
1219