C++ solution 8ms with detailed explanation
\nThe way to solve this question is using a stack.\nWe initally push an element into tthe stack(non zero).\nWhen we encounter the next element we check if it is
```\nThe way to solve this question is using a stack.\nWe initally push an element into tthe stack(non zero).\nWhen we encounter the next element we check if it is smaller than the element present in the stack if so pop the element from\nthe stack and push this element.Why? because he current element has the cacpacity of producting a smaller number as compared to the latter.But we have to limit the pops upto k(cuz only k deletions allowed).\nLet us use an example to understand it better:-\nnum = "1432219", k = 3\nwe first push 1 into the stack\n           |         |\n           |         |\n           |         |   <-----Stack representation\n           |   1     |\n           |_________|\n\nnow we encounter 4 we check if 4 is lesser than 1 as it is not we push into the stack.\n\n           |         |\n           |         |\n           |         |   <-----Stack representation\n           |   4     |\n           |___1_____|\n\nthen 3 comes as it is lesser than the top of the stack we pop 4 out and we push 3 in while simultaneously decrementing k . \n\n           |         |\n           |         |\n           |         |   <-----Stack representation  k=2\n           |   3     |\n           |___1_____|\n\n2 beats 3 so 3 is popped out and 2 is pushed and k is decremented.\n\n           |         |\n           |         |\n           |         |   <-----Stack representation  k=1\n           |   2     |\n           |___1_____|\n2 doesnt beat 2 so it is pushed into the stack\n\n           |         |\n           |         |\n           |   2     |   <-----Stack representation\n           |   2     |\n           |___1_____|\n1 beats 2 so 2 is popped out and 1 is pushed in.\n\n           |         |\n           |         |\n           |   1     |   <-----Stack representation   k=0\n           |   2     |\n           |___1_____|    \nnow we have the push whatever number is left as we have used up all our deletions\n\n           |         |\n           |   9     |\n           |   1     |   <-----Stack representation\n           |   2     |\n           |___1_____|\n\n           answer:-1219.\n\nWe will take another example to make sure our algorithm doesnt have leading 0s.\n\nnum = "10200", k = 1\n\n           |         |\n           |         |\n           |         |   <-----Stack representation\n           |   1     |\n           |_________|\n0 beats 1 but we should push 0 as this will cause leading zeros so we push the element if(ans.size()||c!=\'0\') meaning\nif ans.size()==0 and c==\'0\' it wouldnt push.\n\nbut we wedelete 1 as we have to fulfil the condition of the number of deleteions.\n\n           |         |\n           |         |\n           |         |   <-----Stack representation   k=0\n           |   2     |\n           |_________|\n\n           push  rest of the elements by making use of if(ans.size()||c!=\'0\')push(element).\n\n\n\n           |         |\n           |   0     |\n           |   0     |   <-----Stack representation   k=0\n           |   2     |\n           |_________|\n\n\n           answer is 200\nFor optimization purposes we use a string as stack (s.push_back() and s.pop_back()).\n\n    string removeKdigits(string num, int k) {\n        string ans="";\n        for(char &c:num)\n        {\n            while(ans.size() && ans.back()>c &&k)\n            {\n                ans.pop_back();\n                k--;\n            }\n            if(ans.size()||c!=\'0\')ans.push_back(c);\n        }\n        while(ans.size()&&k--)           <-------this look is to make sure we fulfil the deletion condition\n        {\n            ans.pop_back();\n        }\n        return (ans=="")?"0":ans;\n    }\n```
['C', 'C++']