[題目]
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.
[Solution]
Use a stack with N-K size to store the answer. Bottom is most sign digit. Top is less sign digit.
1.Since original size of number is N, and we can remove K digits from it. The best choice is to remove exactly K digits. It makes the answer lower.
2.Use a stack to put answer. Stack bottom represent the most sign digit. Stack top represent the less sign digit.
Decide digit[0]~digit[i] should stay or not according to digit[i+1].
New we are pushing digit into stack.
1.When push d[0] to stack, we don't know if d[0] is a good choice for most sign digit or not. But let's assume it is (we don't have choice actually, we can't just remove d[0] without more information). So we just push d[0] into stack, and wait for more info.
2.When push d[1] to stack, there is two condition :
a. d[0] <= d[1] : d[0] is lower, so d[0] is better than d[1] to be a most sign digit. Then we just simply push(d[1]).
b. d[0] > d[1] : d[1] is lower than d[0], which makes it a better choice for that slot, so we let d[1] take d[0]'s place.
We pop d[0] and push d[1], so d[1] replace d[0] and become bottom of stack, and most sign digit.
2.b is the most important step. The core concept is : "We should not allow the big number take the most sign place. So if d[i+1] is smaller(better) than d[i](which is at stack.top()), we should pop d[i] out. Furthermore, we should pop stack continuously until stack.top() is smaller or equal to d[i+1]. "
[code]
public:
string removeKdigits(string num, int k) {
string maxS;
for (auto ch : num){
while(!maxS.empty() && maxS.back() > ch && k > 0){
maxS.pop_back();
k--;
}
maxS.push_back(ch);
}
//if still barging chip K left, then we start pop from back(the most biggest number)
while (k-->0) maxS.pop_back();
bool foundFirstNonZero=false;
string ans;
for (auto ch : maxS){
if(ch!='0') foundFirstNonZero=true;
if(foundFirstNonZero){
ans +=ch;
}
}
return ans.empty() ? "0" : ans;
}
};