Skip to content

Commit 3468d45

Browse files
committed
Runtime: 102 ms (Top 90.00%) | Memory: 56.1 MB (Top 32.50%)
1 parent beb5ed1 commit 3468d45

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1+
// Runtime: 102 ms (Top 90.00%) | Memory: 56.1 MB (Top 32.50%)
12
var minSwaps = function(nums) {
23
const MAX = Number.MAX_SAFE_INTEGER;
3-
4+
45
let ones = nums.reduce((acc, bit) => acc + bit, 0);
5-
6+
67
const doubledNums = nums.concat(nums.slice(0, nums.length - 1));
7-
8+
89
let minSwap = MAX;
910
let left = 0;
1011
let onesWithinWindow = 0;
11-
12+
1213
for (let i = 0; i < doubledNums.length; ++i) {
1314
const rightBit = doubledNums[i];
14-
15+
1516
if (rightBit === 1) onesWithinWindow += 1;
16-
17+
1718
if (i >= ones) {
1819
const leftBit = doubledNums[left];
19-
20+
2021
if (leftBit === 1) onesWithinWindow -= 1;
2122
++left;
2223
}
23-
24+
2425
if (i + 1 >= ones) minSwap = Math.min(minSwap, ones - onesWithinWindow);
2526
}
26-
27+
2728
return minSwap;
28-
};
29+
};

0 commit comments

Comments
 (0)