This is a typical monotonic queue question. Share my c++ solution
It is kinds of greedy idea. As long as we have enough elements, we should pop_back the rear element of string res, which is larger than current element num[i]. 
It is kinds of greedy idea. As long as we have enough elements, we should pop_back the rear element of string res, which is larger than current element num[i]. It is because we can guarantee that the new res after push num[i] is smaller than before.\n```c++\n    string removeKdigits(string num, int k) {\n        string res="";\n        if(num.size()==k)\n            return "0";\n        for(int i=0;i<num.size();i++){\n            while(!res.empty()&&res.back()>num[i]&&k>0){\n                res.pop_back();\n                k--;\n            }\n            res.push_back(num[i]);\n        }\n        auto pos=res.find_first_not_of('0');\n        if(pos==string::npos)\n            return "0";\n        return res.substr(pos,num.size()-k);\n    }\n```
['Greedy', 'C', 'Monotonic Queue']