<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:
        
        num = [-1] + [int(el) for el in num]

        tree = {i:0 for i in range(1, len(num))}

        def lsb(n):
            ret = n&(-n)
            return ret

        def query(idx):
            oute = 0
            while idx > 0:
                oute += tree[idx]
                idx -= lsb(idx)
            return oute

        def update(idx, val):
            while idx in tree:
                tree[idx] += val
                idx += lsb(idx)
        
        
        

        adj = {i:[] for i in range(0, 10)}

        #print(num)
        #print(' ')

        for i, pos in enumerate(num):
            if pos in adj:
                adj[pos].append((i, i))
        for pos in adj:
            heapq.heapify(adj[pos])
            #print(pos, adj[pos])
        
        visited = set()
        adjtot = [(i, i) for i in range(1, len(num))]
        heapq.heapify(adjtot)

        i = 1
        oute = []
        
        while len(oute) < len(num)-1:
            while adjtot and adjtot[0][1] in visited:
                heapq.heappop(adjtot)
            while adjtot and adjtot[0][0] != query(adjtot[0][1]) + adjtot[0][1]:
                val = heapq.heappop(adjtot)
                pus = (query(val[1]) + val[1], val[1])
                #print(f'!!!!, val = {val}, pus = {pus}')
                heapq.heappush(adjtot, pus)
            
            i = adjtot[0][1]
            pos = num[i]
            
            flag = 0
            queryi = query(i)
            
            for swapper in range(0, pos+1):
                
                while adj[swapper] and adj[swapper][0][0] != query(adj[swapper][0][1]) + adj[swapper][0][1]:
                    val = heapq.heappop(adj[swapper])
                    heapq.heappush(adj[swapper], (val[1] + query(val[1]), val[1]))
                while adj[swapper] and adj[swapper][0][0] < queryi:
                    heapq.heappop(adj[swapper])
                if not adj[swapper] or adj[swapper][0][0] - i - queryi > k:
                    continue
                else:
                    val = heapq.heappop(adj[swapper])
                    oute.append(num[val[1]])
                    k -= val[0] - i - queryi
                    update(i, 1)
                    update(val[1], -1)
                    visited.add(val[1])
                    flag = 1
                    
                    break

            if flag == 0:
                oute.append(pos)
                visited.add(i)
                

            '''print('')
            print(f'adjtot: {adjtot}, visited: {visited}')
            print(f'i: {i}, k: {k}, queryi: {queryi}, pos: {pos}, flag: {flag}, oute: {oute}')
            print('')
            for posi in adj:
                print(posi, adj[posi])'''
        
        return ''.join([str(el) for el in oute])
29400