Skip to content

Commit 72dcc22

Browse files
authored
Update minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.cpp
1 parent 375e5e6 commit 72dcc22

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

C++/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ class BIT { // Fenwick Tree, 1-indexed
3232
class Solution {
3333
public:
3434
string minInteger(string num, int k) {
35-
unordered_map<int, deque<int>> lookup;
35+
unordered_map<int, vector<int>> lookup;
3636
BIT bit(num.length() + 1);
37-
for (int i = 0; i < num.length(); ++i) {
37+
for (int i = num.length() - 1; i >= 0; --i) {
3838
lookup[num[i] - '0'].emplace_back(i + 1);
3939
bit.add(i + 1, 1);
4040
}
4141
string result;
4242
for (int i = 0; i < num.length(); ++i) {
4343
for (int d = 0; d <= 9; ++d) {
44-
if (!lookup[d].empty() && bit.sum(lookup[d].front() - 1) <= k) {
45-
k -= bit.sum(lookup[d].front() - 1);
46-
bit.add(lookup[d].front(), -1);
47-
lookup[d].pop_front();
44+
if (!lookup[d].empty() && bit.sum(lookup[d].back() - 1) <= k) {
45+
k -= bit.sum(lookup[d].back() - 1);
46+
bit.add(lookup[d].back(), -1);
47+
lookup[d].pop_back();
4848
result.push_back('0' + d);
4949
break;
5050
}

0 commit comments

Comments
 (0)