-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumPossibleIntegerAfteratMostKAdjacentSwapsOnDigits.py
More file actions
52 lines (45 loc) · 1.37 KB
/
minimumPossibleIntegerAfteratMostKAdjacentSwapsOnDigits.py
File metadata and controls
52 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/
# Author: Miao Zhang
# Date: 2021-05-12
class BIT:
def __init__(self, n: int) -> None:
self.sums = [0] * (n + 1)
def update(self, i: int, delta: int) -> None:
i += 1
while i < len(self.sums):
self.sums[i] += delta
i += i & -i
def query(self, i: int) -> int:
i += 1
res = 0
while i > 0:
res += self.sums[i]
i -= i & -i
return res
class Solution:
def minInteger(self, num: str, k: int) -> str:
n = len(num)
pos = [[] for _ in range(10)]
for i in range(n):
pos[int(num[i])].append(i)
tree = BIT(n)
seen = [0] * n
res = ''
while k > 0 and len(res) < n:
for d in range(10):
if not pos[d]: continue
i = pos[d][0]
cost = i - tree.query(i - 1)
if cost > k: continue
k -= cost
res += str(d)
tree.update(i, 1)
seen[i] = 1
pos[d].pop(0)
break
for i in range(n):
if not seen[i]:
res += str(num[i])
return res