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

class FenwickTree:
    def __init__(self, n):
        self.sums = [0] * (n + 1)

    def add(self, i, delta):
        while i < len(self.sums):
            self.sums[i] += delta
            i += self.lowbit(i)

    def get(self, i):
        total = 0
        while i > 0:
            total += self.sums[i]
            i -= self.lowbit(i)
        return total

    @staticmethod
    def lowbit(i):
        return i & -i

class Solution:
    def minInteger(self, num: str, k: int) -> str:
        n = len(num)
        ans = []
        tree = FenwickTree(n)
        used = [False] * n
        num_to_indices = [deque() for _ in range(10)]

        # Populate the deque with indices for each digit
        for i, char in enumerate(num):
            num_to_indices[int(char)].append(i)

        while k > 0 and len(ans) < n:
            for d in range(10):
                if not num_to_indices[d]:
                    continue
                i = num_to_indices[d][0]
                cost = i - tree.get(i + 1)  # 1-based index adjustment
                if cost > k:
                    continue
                k -= cost
                ans.append(str(d))
                used[i] = True
                tree.add(i + 1, 1)  # 1-based index adjustment
                num_to_indices[d].popleft()
                break  # Restart from 0 -> 9

        # Append the remaining unused characters
        for i in range(n):
            if not used[i]:
                ans.append(num[i])

        return ''.join(ans)

19800