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

#define PLACEHOLDER '?'

struct Node {
    int weight; 
    int val;
    Node* left; 
    Node* right; 

    Node(int val) : weight(1), val(val), left(nullptr), right(nullptr) {}
};

class BST {
    private:
        Node* root; 

        void del(Node* curr) {
            if (curr == nullptr) {
                return;
            }
            del(curr->left);
            del(curr->right);
            delete curr;
        }

        Node* _insert(Node* curr, int val) {
            if (curr == nullptr) {
                return new Node(val);
            }
            curr->weight = curr->weight + 1;
            if (curr->val > val) {
                curr->left = _insert(curr->left, val);
            } else {
                curr->right = _insert(curr->right, val); 
            }
            return curr;
        }

        int _getSmaller(Node* curr, int val) {
            if (curr == nullptr) {
                return 0; 
            }
            if (curr->val > val) {
                return _getSmaller(curr->left, val);
            } 
            int result = 1; 
            if (curr->left) {
                result += curr->left->weight; 
            }
            result += _getSmaller(curr->right, val);
            return result;
        }
    
    public: 
        void insert(int val) {
            this->root = this->_insert(root, val);
        }
        int getSmaller(int val) {
            return this->_getSmaller(this->root, val);
        }
        ~BST() {
            this->del(this->root);
        }
        int getSize() {
            return this->root != nullptr ? this->root->weight : 0;
        }
};

class Solution {
public:
    string minInteger(string num, int k) {
        BST usedPositions{};
        vector<queue<int>> pos(10, queue<int>{});
        for (int i = 0; i < num.size(); i++) {
            pos[num[i] - '0'].push(i);
        }

        string res(num.size(), PLACEHOLDER);
        vector<bool> isUsed (num.size(), false);
        while (k > 0) {
            bool hasInserted = false;
            for (int i = 0; i < 10; i++) {
                if (pos[i].empty()) {
                    continue;
                }
                int potentialPos = pos[i].front();
                int shiftedLeft = usedPositions.getSmaller(potentialPos);
                if (potentialPos - shiftedLeft > k) {
                    continue;
                }
                k -= (potentialPos - shiftedLeft);
                res[usedPositions.getSize()] = num[potentialPos];
                usedPositions.insert(potentialPos);
                isUsed[potentialPos] = true;
                pos[i].pop();
                hasInserted = true;
                break;
            }
            if (!hasInserted) {
                break;
            }
        }
        int currInserted = usedPositions.getSize();
        for (int i = 0; i < num.size(); i++) {
            if (isUsed[i]) {
                continue;
            }
            res[currInserted++] = num[i];
        }
        return res;
    }
};
19900