<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:
    def find_first(l, a): # assume l is sorted
        start =0 
        end = len(l)-1
        while start <= end:
            mid = start + (end-start)//2
            if l[mid] == a and mid ==0:
                return mid
            if l[mid]==a and l[mid-1] != a:
                return mid
            if l[mid] < a:
                if start == mid+1:
                    break
                start = mid +1
            elif l[mid-1] >= a:
                if end == mid-1:
                    break
                end = mid-1
        
        return None
             
    def minInteger(self, num, k):
        n = len(num)
        num = [(i, int(elem)) for i, elem in enumerate(num)]
        
        digit_lists = [[] for _ in range(10)]
        
        for i in range(len(num)):
            digit_lists[num[i][1]].append(i)


        for insert_ind in range(0, len(num)):
            # print([elem[1] for elem in num])
            num2 = num[insert_ind:]
            
            del_digit = num[insert_ind][1]
            
            for dig in range(del_digit):
                if len(digit_lists[dig]) == 0:
                    continue
                
                # print(num2[:k+1], (digit_lists[dig][0], dig), k )
                i = Solution.find_first(num2[:k+1], (digit_lists[dig][0], dig))
                if i is not None:
                    a = num2[i]
                    del_digit = a[1]
                    del num[insert_ind + i]
                    num.insert(insert_ind, a)
                    k -= i
                    break
            del digit_lists[del_digit][0]            
        return ''.join([str(elem[1]) for elem in num])


22400