<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>

from collections import defaultdict
from sortedcontainers import SortedList

class SegTree:
    def __init__(self, n):
        self.ctr = defaultdict(int)
        self.init = (float('inf'), n)
        self.vals = defaultdict(lambda: self.init)
        
    def update(self, i, v, x, y, k = 1):
        if x + 1 == y:
            self.ctr[k] = 0 if v == -1 else 1
            self.vals[k] = (float('inf'), i) if v == -1 else (v, i)
            return
        mid = (x + y) // 2
        if i < mid:
            self.update(i, v, x, mid, 2 * k)
        else:
            self.update(i, v, mid, y, 2 * k + 1)
        self.ctr[k] = self.ctr[2 * k] + self.ctr[2 * k + 1]
        self.vals[k] = min(self.vals[2 * k], self.vals[2 * k + 1])
        
    def getMin(self, p, x, y, k = 1):
        if p == 0:
            return self.init
        if x + 1 == y:
            return self.vals[k]
        mid = (x + y) // 2
        res = self.init
        if p >= self.ctr[2 * k]:
            res = min(res, self.vals[2 * k], self.getMin(p - self.ctr[2 * k], mid, y, 2 * k + 1))
        else:
            res = min(res, self.getMin(p, x, mid, 2 * k))
        return res

class Solution:
    def minInteger(self, num: str, k: int) -> str:
        n = len(num)
        segtree = SegTree(n)
        for i in range(n):
            segtree.update(i, int(num[i]), 0, n)
        w = k + 1
        res = []
        bst = SortedList()
        while len(res) < n:
            val, j = segtree.getMin(min(w, n), 0, n)
            res.append(num[j])
            pos = j - bst.bisect_right(j - 1)
            bst.add(j)
            w -= pos
            segtree.update(j, -1, 0, n)
        return "".join(res)
57000