-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1505-minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js
More file actions
58 lines (52 loc) · 1.47 KB
/
1505-minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js
File metadata and controls
58 lines (52 loc) · 1.47 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
53
54
55
56
57
58
/**
* 1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits
* https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/
* Difficulty: Hard
*
* You are given a string num representing the digits of a very large integer and an integer k. You
* are allowed to swap any two adjacent digits of the integer at most k times.
*
* Return the minimum integer you can obtain also as a string.
*/
/**
* @param {string} num
* @param {number} k
* @return {string}
*/
var minInteger = function(num, k) {
const digits = num.split('');
const n = digits.length;
const fenwick = new Array(n + 1).fill(0);
const digitQueues = Array.from({ length: 10 }, () => []);
const result = [];
for (let i = 0; i < n; i++) {
digitQueues[digits[i]].push(i);
}
for (let i = 0; i < n; i++) {
for (let digit = 0; digit <= 9; digit++) {
if (digitQueues[digit].length === 0) continue;
const pos = digitQueues[digit][0];
const swapsNeeded = pos - fetchValue(pos);
if (swapsNeeded <= k) {
k -= swapsNeeded;
result.push(digit);
update(pos);
digitQueues[digit].shift();
break;
}
}
}
return result.join('');
function update(index) {
for (let i = index + 1; i <= n; i += i & -i) {
fenwick[i]++;
}
}
function fetchValue(index) {
let sum = 0;
for (let i = index + 1; i > 0; i -= i & -i) {
sum += fenwick[i];
}
return sum;
}
};