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

/* 
    Time: O(nlogn)
    Space: O(n)
    Tag: Segment Tree, Greedy (put the possible feasible smallest index in place of cur index), Queue
    Difficulty: H (Both Logic and Implementation)
*/

class SegmentTree {
    vector<int> tree;

public:
    SegmentTree(int size) {
        tree.resize(4 * size + 1, 0);
    }

    void printTree() {
        for (int num : tree) cout << num << "";
        cout << endl;
    }

    void updateTree(int lo, int hi, int index, int upd) {
        if (upd < lo || upd > hi) return;
        if (lo == hi) {
            tree[index]++;
            return;
        }
        int mid = lo + (hi - lo) / 2;
        updateTree(lo, mid, 2 * index, upd);
        updateTree(mid + 1, hi, 2 * index + 1, upd);
        tree[index] = tree[2 * index] + tree[2 * index + 1];
    }

    int queryTree(int lo, int hi, int index, int qs, int qe) {
        if (qe < lo || qs > hi) return 0;
        if (qe >= hi && qs <= lo) return tree[index];

        int mid = lo + (hi - lo) / 2;

        int left = queryTree(lo, mid, 2 * index, qs, qe);
        int right = queryTree(mid + 1, hi, 2 * index + 1, qs, qe);
        return left + right;
    }
};

class Solution {
public:
    string minInteger(string num, int k) {
        queue<int> pos[10];
        for (int i = 0; i < num.length(); i++) {
            pos[num[i] - '0'].push(i);
        }
        string res = "";
        SegmentTree *seg = new SegmentTree((int)num.length());
        for (int i = 0; i < num.length(); i++) {
            if (num[i] == '-') continue;
            int digit = num[i] - '0';

            bool swapped = false;
            for (int j = 0; j < digit; j++) {
                if (pos[j].size() > 0) {
                    int curNumIndex = pos[j].front();
                    int shifts = seg->queryTree(0, num.length() - 1, 1, i, pos[j].front());

                    if (curNumIndex - i - shifts <= k) {
                        seg->updateTree(0, num.length() - 1, 1, curNumIndex);
                        k -= curNumIndex - i - shifts;
                        pos[j].pop();
                        res += num[curNumIndex];
                        num[curNumIndex] = '-';
                        swapped = true;
                        i--;
                        break;
                    }
                }
            }
            if (!swapped) {
                res += num[i];
                pos[digit].pop();
            }
        }
        return res;
    }
};
13800