<p>Given string num representing a non-negative integer <code>num</code>, and an integer <code>k</code>, return <em>the smallest possible integer after removing</em> <code>k</code> <em>digits from</em> <code>num</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;1432219&quot;, k = 3
<strong>Output:</strong> &quot;1219&quot;
<strong>Explanation:</strong> Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;10200&quot;, k = 1
<strong>Output:</strong> &quot;200&quot;
<strong>Explanation:</strong> Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> num = &quot;10&quot;, k = 2
<strong>Output:</strong> &quot;0&quot;
<strong>Explanation:</strong> Remove all the digits from the number and it is left with nothing which is 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= num.length &lt;= 10<sup>5</sup></code></li>
	<li><code>num</code> consists of only digits.</li>
	<li><code>num</code> does not have any leading zeros except for the zero itself.</li>
</ul>

class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        '''
        bigger or equal to right digit, then delete
        '''
        # mono stack
        monostack = []
        i = 0
        while i < len(num):
            incoming = int(num[i])
            # print(monostack, incoming, k)
            while monostack and monostack[-1] > incoming and k > 0:
                monostack.pop()
                k -= 1
            # skip if its 0 and when stack empty
            if incoming or (not incoming and monostack):
                monostack.append(incoming)
            i += 1
        
        while k > 0 and monostack:
            monostack.pop()
            k -= 1
        # remove all the front zeros
        if not monostack:
            return "0"
        return "".join([str(n) for n in monostack])

        



        # i = 0
        # while k > 0 and i + 1 < len(num):
        #     if num[i] > num[i+1]: #delete at i
        #         num = num[:i] + num[i+1:]
        #         # if leading 0, delete
        #         while i == 0 and i < len(num) and num[i] == "0":
        #             num = num[1:]
        #         k -= 1
        #         i -= 1 if i > 0 else 0
        #     else:
        #         i += 1
        # #at the end, possible that still have k, then just delete all of them
        # if (k > 0 and k >= len(num)) or not num:
        #     return "0"
        # elif k > 0: #delete the right side # delete from top
        #     return num[:len(num)-k]
        # else:
        #     return num


20600