Remove K Digits - Complete Solution Guide
Remove K Digits is LeetCode problem 402, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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",
Detailed Explanation
The problem requires you to remove *k* digits from a given non-negative integer represented as a string *num*, such that the resulting number is the smallest possible. The output should also be a string and must not contain leading zeros, except for the number zero itself.
Solution Approach
The solution uses a greedy approach and a stack data structure. It iterates through the digits of the input number *num*. For each digit, it compares it with the top of the stack. If the current digit is smaller than the top of the stack, and we still have removals (*k* > 0) allowed, we pop the stack. This process continues until either the stack is empty, *k* becomes 0, or the top of the stack is no longer greater than the current digit. Then, the current digit is pushed onto the stack. After processing all digits, if *k* is still greater than 0, it means the remaining digits in the stack are in non-decreasing order, so we remove the last *k* digits. Finally, we convert the stack to a string, remove any leading zeros, and return the result. If the result is empty, we return "0".
Step-by-Step Algorithm
- Step 1: Initialize an empty stack to store digits.
- Step 2: Iterate through each digit in the input string *num*.
- Step 3: While the stack is not empty, *k* > 0, and the top of the stack is greater than the current digit, pop the top element from the stack and decrement *k*.
- Step 4: Push the current digit onto the stack.
- Step 5: After iterating through all digits, if *k* is still greater than 0, remove the last *k* digits from the stack.
- Step 6: Convert the stack to a string.
- Step 7: Remove any leading zeros from the resulting string.
- Step 8: If the resulting string is empty, return "0"; otherwise, return the string.
Key Insights
- Insight 1: The greedy approach of iteratively removing larger digits from left to right is optimal for minimizing the resulting number.
- Insight 2: A stack data structure is suitable for maintaining a monotonically increasing (or non-decreasing) sequence of digits.
- Insight 3: Handling edge cases such as leading zeros and situations where *k* is greater than or equal to the length of *num* is critical.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String, Stack, Greedy, Monotonic Stack.
Companies
Asked at: Cisco, Coupang, DE Shaw, PhonePe, Samsung, Snap, Snowflake, Zoho, Zopsmart, josh technology.