This is one of those problems where the clean idea matters more than the amount of code. For 402. Remove K Digits, the solution in this repository is mainly a stack solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack, greedy.
The notes already sitting in the source point us in the right direction:
- stack, greedy
- https://leetcode.com/problems/remove-k-digits/discuss/88708/Straightforward-Java-Solution-Using-Stack
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are removeKdigits.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//stack, greedy
02//https://leetcode.com/problems/remove-k-digits/discuss/88708/Straightforward-Java-Solution-Using-Stack
03//Runtime: 8 ms, faster than 62.37% of C++ online submissions for Remove K Digits.
04//Memory Usage: 7.1 MB, less than 100.00% of C++ online submissions for Remove K Digits.
05class Solution {
06public:
07 string removeKdigits(string num, int k) {
08 if(k >= num.size()) return "0";
09
10 stack<int> stk;
11
12 for(int i = 0; i < num.size(); i++){
13 //k > 0: only discard char when k> 0
14 //it's "while", not "if" here!!
15 //it's ">", not ">=" here!!
16 while(k > 0 && !stk.empty() && stk.top() > num[i]){
17 stk.pop(); //discard the larger and the former char
18 k--;
19 }
20
21 stk.push(num[i]);
22 }
23
24 while(k-- > 0){
25 stk.pop();
26 }
27
28 string ans = "";
29
30 while(!stk.empty()){
31 //56ms
32 // ans = string(1, stk.top()) + ans;
33 //8ms!!
34 ans += stk.top();
35 stk.pop();
36 }
37
38 //8ms!!
39 std::reverse(ans.begin(), ans.end());
40
41 //remove leading 0
42 while(ans.size() > 1 && ans[0] == '0'){
43 ans.erase(0, 1);
44 }
45
46 return ans;
47 }
48};
Cost