Greedy Algorithms8 sections · 316 units
Open in Course

Problem - Remove K Digits

Minimize the number

Remove k digits from a number string to make it as small as possible.

Example: num = "14322191432219", k = 33. Answer: "12191219". Remove 44, 33, 22. The greedy insight: to get a smaller number, we want leftmost digits to be as small as possible. Tricky: after removing digits, the result might have leading zeros like '00320032' which should become '3232'. Edge case: removing all digits leaves '00', not an empty string. The greedy choice applies left to right because leftmost digits have highest positional value.