402. Remove K Digits
Remove K Digits (LeetCode 402) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Stack Patterns pattern, in the Monotonic Stack subpattern.
How to solve Remove K Digits
Remove K Digits is a Monotonic Stack problem, part of Stack Patterns. Master monotonic stack for next greater element, daily temperatures, and stock span problems.
Work towards the solution in these steps:
- Consider which data structure can help you efficiently decide which digits to remove when trying to form the smallest number.
- Think about using a stack to build the result, and how you might maintain an order to ensure the smallest possible digits remain.
- Focus on maintaining a monotonic increasing stack, where you pop from the stack when the current digit is smaller than the top of the stack and you still have removals left.
The reference solution runs in O(n) time and O(n) space.
The Remove K Digits solution and editorial walks through the full approach with reference code.
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", 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 <= 105
- num consists of only digits.
- num does not have any leading zeros except for the zero itself.
Topics and companies
Topics: String, Stack, Greedy, Monotonic Stack.
Reported in interviews at Amazon, Microsoft, Adobe, Oracle.
How to practise Remove K Digits on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.
Open Remove K Digits in the code editor, or read the Remove K Digits editorial for a worked solution with its approach and complexity analysis.
Where Remove K Digits sits in the DSA pattern sheet
Problems related to Remove K Digits
Other problems that use the same Monotonic Stack and Stack Patterns techniques:
- Car Fleet — Medium, same subpattern
- Daily Temperatures — Medium, same subpattern
- Final Prices With a Special Discount in a Shop — Easy, same subpattern
- Find the Most Competitive Subsequence — Medium, same subpattern
- Maximum Width Ramp — Medium, same subpattern
- Next Greater Element I — Easy, same subpattern
Remove K Digits FAQ
What is the LeetCode number of Remove K Digits?
Remove K Digits is LeetCode problem 402, rated Medium.
What pattern is used to solve Remove K Digits?
Remove K Digits is a Monotonic Stack problem. It belongs to the Stack Patterns pattern, in the Monotonic Stack subpattern.
What is the time complexity of Remove K Digits?
The reference solution runs in O(n) time and O(n) space.
Which companies ask Remove K Digits?
Remove K Digits has been reported in interviews at Amazon, Microsoft, Adobe, Oracle.