Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.
Example 1:
Input: num = "1432219", k = 3 Output: "1219" Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1 Output: "200" Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2 Output: "0" Explanation: Remove all the digits from the number and it is left with nothing which is 0.
Constraints:
1 <= k <= num.length <= 105num consists of only digits.num does not have any leading zeros except for the zero itself.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a long number and you need to erase a specific amount of digits to make the remaining number as small as possible. The brute force approach involves exploring every single possible combination of digits you could choose to erase.
Here's how the algorithm would work step-by-step:
import itertools
def remove_k_digits_brute_force(num_string, digits_to_remove):
# Generate all unique index combinations to ensure we check every removal possibility
index_combinations = itertools.combinations(
range(len(num_string)), len(num_string) - digits_to_remove
)
possible_numbers = []
for current_combination in index_combinations:
constructed_number = "".join(num_string[index] for index in current_combination)
possible_numbers.append(constructed_number)
# Filter out leading zeros and handle empty string edge case for proper numeric comparison
cleaned_numbers = [
number.lstrip("0") if number.lstrip("0") else "0" for number in possible_numbers
]
# Sort numerically based on length first then string value to correctly find the absolute minimum
cleaned_numbers.sort(key=lambda item: (len(item), item))
# Return the absolute smallest number from our complete collection of possibilities
return cleaned_numbers[0] if cleaned_numbers else "0"To find the smallest possible number after removing a specific amount of digits, we want to build our answer from left to right while keeping the smaller digits as far left as possible. We can achieve this by comparing each new digit to the ones we have already kept and tossing out larger digits if a smaller one comes along.
Here's how the algorithm would work step-by-step:
def remove_k_digits(numeric_string, digits_to_remove):
holding_space = []
for current_digit in numeric_string:
# Remove larger previous digits so smaller digits occupy more significant left positions.
while digits_to_remove > 0 and holding_space and holding_space[-1] > current_digit:
holding_space.pop()
digits_to_remove -= 1
holding_space.append(current_digit)
# If removals remain, trim from the right since those are the largest magnitude digits.
if digits_to_remove >:
holding_space = holding_space[:-digits_to_remove]
final_number_string = "".join(holding_space).lstrip("0")
# Return zero if the string became empty after trimming leading zeros and digits.
return final_number_string if final_number_string else "0"| Case | How to Handle |
|---|---|
| Removing all digits where k equals the length of num | Return a zero string since removing every digit leaves nothing, and an empty result should default to 0. |
| Removing zero digits where k is 0 | Return the original string as no digits need to be removed. |
| Leading zeros in the resulting number after removals | Strip all leading zeros from the final accumulated stack before returning, ensuring standard integer representation. |
| Monotonically increasing digits like 12345 with k equals 2 | The greedy stack approach will naturally pop the largest elements from the end of the string. |
| Monotonically decreasing digits like 54321 with k equals 2 | The monotonic stack continuously pops larger elements until k runs out, leaving the smallest remaining suffix. |
| All digits are identical such as 1111 with k equals 2 | The algorithm removes the first k digits from the left since no smaller predecessor exists to trigger a pop. |
| String length is at the maximum constraint limit of 100000 | A linear time stack-based approach processes the string in O(N) time to efficiently scale within time limits. |
| Resulting string becomes completely empty after stripping leading zeros | Return a single zero character to satisfy the requirement that the output represents a valid non-negative integer. |