<p>You are given a string <code>num</code> representing <strong>the digits</strong> of a very large integer and an integer <code>k</code>. You are allowed to swap any two adjacent digits of the integer <strong>at most</strong> <code>k</code> times.</p>

<p>Return <em>the minimum integer you can obtain also as a string</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/06/17/q4_1.jpg" style="width: 500px; height: 40px;" />
<pre>
<strong>Input:</strong> num = &quot;4321&quot;, k = 4
<strong>Output:</strong> &quot;1342&quot;
<strong>Explanation:</strong> The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;100&quot;, k = 1
<strong>Output:</strong> &quot;010&quot;
<strong>Explanation:</strong> It&#39;s ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;36789&quot;, k = 1000
<strong>Output:</strong> &quot;36789&quot;
<strong>Explanation:</strong> We can keep the number without any swaps.
</pre>

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

<ul>
	<li><code>1 &lt;= num.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>num</code> consists of only <strong>digits</strong> and does not contain <strong>leading zeros</strong>.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minInteger(self, num: str, k: int) -> str:
        """
            Keep taking the smallest digit and moving it to the left. For 2 
            equal smallest digits, take the digit that is further to the left.
        """
        n = len(num)
        
        t=[0]*(n*4)  # segment tree
        # when calling each function, use v=1,tl=0,tr=n-1
        def build(a,v,tl,tr):
            if tl==tr:
                t[v]=a[tl]
            else:
                tm=(tl+tr)//2
                build(a,v*2,tl,tm)
                build(a,v*2+1,tm+1,tr)
                t[v]=t[v*2]+t[v*2+1]
        def query(v,tl,tr,l,r):
            if l>tr or r<tl:
                return 0
            elif l<=tl and tr<=r:
                return t[v]
            else:
                tm=(tl+tr)//2
                return query(v*2,tl,tm,l,r)+query(v*2+1,tm+1,tr,l,r)
        def update(v,tl,tr,pos,new_val):
            if tl==tr:
                t[v]=new_val
            else:
                tm=(tl+tr)//2
                if pos<=tm:
                    update(v*2,tl,tm,pos,new_val)
                else:
                    update(v*2+1,tm+1,tr,pos,new_val)
                t[v]=t[v*2]+t[v*2+1]
        a = [1] * n
        build(a, 1, 0, n - 1)

        
        indexes = [[] for _ in range(10)]
        for i, x in enumerate(num):
            indexes[int(x)].append(i)
        for arr in indexes:
            arr.reverse()
        
        ans_arr = [-1] * n
        for i in range(n):
            # find the smallest j that can be used
            for j in range(10):
                if len(indexes[j]) > 0:
                    i2 = indexes[j][-1]
                    if i2 == 0:
                        swaps_needed = 0
                    else:
                        swaps_needed = query(1, 0, n - 1, 0, i2 - 1)
                    if swaps_needed <= k:
                        ans_arr[i] = j
                        indexes[j].pop()
                        update(1, 0, n - 1, i2, 0)
                        k -= swaps_needed
                        break
        ans = ''.join([str(x) for x in ans_arr])
        return ans
            
25000