Remove K Digits
Problem Description
Given string num representing a non-negative integer and an integer k, return the smallest possible integer after removing exactly k digits from num. Return as a string without leading zeros (return '0' if result is empty).
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Example 2:
Input: num = "10200", k = 1
Output: "200"
Example 3:
Input: num = "10", k = 2
Output: "0"
DE Shaw Tier 1 — Lex smallest string after k deletions (Jan 2025).
Optimal: Monotonic non-decreasing stack O(n). When current digit < stack top and k > 0, pop (delete the larger digit). If k remains after scan, pop from end. Strip leading zeros.
Talk-track: 'Each character should be as small as possible at the earliest position. Greedy: removing a larger digit before a smaller one always improves the prefix.'
Example Test Cases
Example 1
Input: ["1432219",3]
Expected: "1219"
Example 2
Input: ["10200",1]
Expected: "200"
Example 3
Input: ["10",2]
Expected: "0"
Example 4
Input: ["9",1]
Expected: "0"
Run your code to see the results