1
from sortedcontainers import SortedList5
def minInteger(self, num: str, k: int) -> str:6
sz, window = len(num), SortedList()7
remainedIndices, poppedIndices = SortedList(range(sz)), []9
while len(window) < k + 1 and len(window) < len(remainedIndices):10
idx = remainedIndices[len(window)]11
window.add((num[idx], idx))14
index = window.pop(0)[1]15
k -= remainedIndices.bisect_left(index)16
remainedIndices.remove(index)17
poppedIndices.append(index)18
for idx in remainedIndices[k + 1 : len(window)]:19
window.remove((num[idx], idx))20
poppedSet = set(poppedIndices)21
return "".join(num[idx] for idx in poppedIndices) + "".join(22
num[idx] for idx in range(sz) if idx not in poppedSet