<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 Solution {
public:
vector<int>seg,lazy;
int query(int ind,int low,int high,int i){
    if(lazy[ind]!=0){
        seg[ind]+=(high-low+1)*lazy[ind];
        if(low!=high){
            lazy[2*ind+1]+=lazy[ind];
            lazy[2*ind+2]+=lazy[ind];
        }
        lazy[ind]=0;
    }
    if(low==high)return seg[ind];
    int mid=(low+high)/2;
    if(i<=mid)return query(2*ind+1,low,mid,i);
    return query(2*ind+2,mid+1,high,i);
}
void update(int ind,int low,int high,int l,int r){
    if(lazy[ind]!=0){
        seg[ind]+=(high-low+1)*lazy[ind];
        if(low!=high){
            lazy[2*ind+1]+=lazy[ind];
            lazy[2*ind+2]+=lazy[ind];
        }
        lazy[ind]=0;
    }
    if(high<l||low>r)return;
    if(low>=l&&high<=r){
        seg[ind]+=(high-low+1);
        if(low!=high){
            lazy[2*ind+1]+=1;
            lazy[2*ind+2]+=1;
        }
        return;
    }
    int mid=(low+high)/2;
    update(2*ind+1,low,mid,l,r);
    update(2*ind+2,mid+1,high,l,r);
}
    string minInteger(string num, int k) {
      vector<int>adjls[10];
      int n=num.size();
      seg.resize(4*n,0);
      lazy.resize(4*n,0);
      for(int i=n-1;i>=0;i--){
          adjls[num[i]-48].push_back(i);
      } 
     
      vector<bool>a(n,false);
      string res; 
      for(int i=0;i<n;i++){
          if(a[i])continue;
          if(k<=0){
              res+=num[i];
              continue;
          }
          bool flag=true;
          int l=num[i]-'0';
          for(int j=0;j<l;j++){
            if(adjls[j].empty())continue;
            int p=adjls[j].back();
            int q=query(0,0,n-1,p);
            int r=query(0,0,n-1,i);
            int swaps=p-i-q+r;
            if(swaps<=k){
                k-=swaps;
                res+=j+'0';
                a[p]=true;
                adjls[j].pop_back();
                flag=false;
                update(0,0,n-1,p,n-1);
                i--;
                break;
            }
          }
          if(flag)res+=num[i],a[i]=true,adjls[l].pop_back();
      }
      cout<<k;
      return res;
    }
};
17600