<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 BIT {
public:
    int n;               // Number of elements
    vector<int> nodes;   // Binary Indexed Tree (Fenwick Tree)

    BIT(int n) {
        this->n = n;
        nodes = vector<int>(n + 1);  // Initialize the BIT with size (n + 1)
    }

    // Function to update a value at index 'i' by adding 'delta'
    void update(int i, int delta) {
        ++i;  // Increment 'i' to convert 0-based indexing to 1-based indexing
        while (i <= n) {
            nodes[i] += delta;  // Update the BIT node at index 'i' by adding 'delta'
            i += (i & -i);     // Move to the parent node (next node with a higher significant bit)
        }
    }

    // Function to query the prefix sum up to index 'i'
    int query(int i) {
        ++i;  // Increment 'i' to convert 0-based indexing to 1-based indexing
        int sum = 0;
        while (i > 0) {
            sum += nodes[i];   // Accumulate the sum from the current node
            i -= (i & -i);    // Move to the previous node (next node with a lower significant bit)
        }
        return sum;
    }
};

class Solution {
public:
    string minInteger(string num, int k) {
        vector<queue<int>> qs(10);  // An array of queues to store positions of each digit
        int n = num.size();

        // Populate queues with the positions of each digit
        for (int i = 0; i < n; ++i) {
            qs[num[i] - '0'].push(i);
        }

        string lhs;  // Left-hand side of the result string
        vector<bool> removed(n, false);  // Indicates if a digit has been removed from the original string
        BIT* tree = new BIT(n);  // Create a Binary Indexed Tree (BIT) to track removed digits

        while (k > 0) {
            bool found = false;
            for (int d = 0; d <= 9; ++d) {
                if (!qs[d].empty()) {
                    int pos = qs[d].front();  // Get the position of the current digit
                    int shifted = tree->query(pos - 1);  // Query the BIT to find the number of removed digits before pos
                    if (pos - shifted <= k) {
                        k -= pos - shifted;  // Update remaining swaps
                        tree->update(pos, 1);  // Mark the current digit as removed in the BIT
                        qs[d].pop();  // Remove the position from the queue
                        lhs += ('0' + d);  // Append the digit to the left-hand side
                        removed[pos] = true;  // Mark the digit as removed
                        found = true;
                        break;
                    }
                }
            }
            if (!found) break;  // If no valid swap is possible, exit the loop
        }

        string rhs;  // Right-hand side of the result string
        for (int i = 0; i < n; ++i) {
            if (!removed[i]) {
                rhs += num[i];  // Append the remaining digits to the right-hand side
            }
        }

        return lhs + rhs;  // Concatenate the left-hand and right-hand sides to form the final result
    }
};
12200