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

template<typename T>
class BIT {
    std::size_t m_N;
    std::vector<T> m_data;

public:
    BIT(std::size_t N) : m_N{N}, m_data(m_N, 0) {}

    void update(int idx, T delta) {
        while(idx < m_N) {
            m_data[idx] += delta;
            idx |= idx+1;
        }
    }

    T sum(int idx) {
        T res = 0;
        while(idx >= 0) {
            res += m_data[idx];
            idx = (idx&(idx+1))-1;
        }
        return res;
    }

    T sum(int from, int to) {
        if (from == 0)
            return sum(to);
        
        return sum(to) - sum(from-1);
    }

    void clear() {
        m_data.assign(m_N, 0);
    }
};
#define ll long long
class Solution {
public:
    string minInteger(string num, int k) {
        int n=num.size();
        BIT<ll> fen(n);
        // for each digit 0-9 we make BIT(freq[digit]) = value(idx)
        vector<int> adj[10];
        for(int i=n-1;i>=0;--i){
            fen.update(i,1);
            adj[num[i]-'0'].push_back(i);
        }
        string ans=num;
        for(int i=0;i<n;i++){
            for(int d=0;d<10;d++){
                if(adj[d].empty() || fen.sum(adj[d].back()) > k+1) continue;
                ans[i]='0'+d;
                k-=fen.sum(adj[d].back())-1; // -1 to swap len - 1
                fen.update(adj[d].back(),-1); // if remove digit then you decrease position to end by -1 and remain from start same 
                // each idx take sum of digit behind it
                adj[d].pop_back();
                break;
            }
        }
        return ans;
    }
};
13100