All problems
Smallest Number After Removing K Digits
Problem
Remove exactly k digits from the decimal string num to produce the smallest possible number, and return it without leading zeros.
Maintain a monotonic increasing stack: pop larger digits while removals remain and the new digit is smaller. After the scan, drop digits from the end if removals remain, strip leading zeros, and return 0 for an all-zero result.
Hint
A bigger digit earlier is always worse, so pop it whenever a smaller digit can replace it.
Test cases
case 1
in: ["1432219",3]
expected: "1219"
case 2
in: ["10200",1]
expected: "200"
case 3
in: ["10",2]
expected: "0"
case 4
in: ["9",1]
expected: "0"
case 5
in: ["112",1]
expected: "11"
Starter codePython
def remove_k_digits(num, k):
# Your code here
pass5 test cases · run your code against them in the editor