-
Notifications
You must be signed in to change notification settings - Fork 3
/
1505.cpp
58 lines (58 loc) · 1.41 KB
/
1505.cpp
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
53
54
55
56
57
58
class BIT {
private:
vector<int> tree;
public:
BIT(int size) {
tree.resize(size + 1, 0);
}
int lsb(int x) {
return x & (-x);
}
void update(int index, int x) {
index += 1;
while(index < tree.size()) {
tree[index] += x;
index += lsb(index);
}
}
int query(int index) {
index += 1;
int sum = 0;
while (index) {
sum += tree[index];
index -= lsb(index);
}
return sum;
}
};
class Solution {
public:
string minInteger(string num, int k) {
int n = num.size();
vector<queue<int>> q(10);
for (int i = 0; i < n; ++i) {
q[num[i] - '0'].push(i);
}
vector<bool> seen(n, false);
BIT* bit = new BIT(n);
string ans;
while (k > 0 && ans.size() < n) {
for (int i = 0; i < 10; ++i) {
if (q[i].empty()) continue;
int index = q[i].front();
int move = index - bit->query(index - 1);
if (move > k) continue;
q[i].pop();
k -= move;
bit->update(index, 1);
ans.push_back(i + '0');
seen[index] = true;
break;
}
}
for (int i = 0; i < n; ++i) {
if (!seen[i]) ans.push_back(num[i]);
}
return ans;
}
};