We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4c1a726 commit cae1481Copy full SHA for cae1481
5977-minimum-swaps-to-group-all-1s-together-ii.js
@@ -0,0 +1,27 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {number}
4
+ */
5
+var minSwaps = function(nums) {
6
+ let one = 0;
7
+ for (let i = 0; i < nums.length; i++) {
8
+ if (nums[i] == 1)
9
+ one++;
10
+ }
11
+ let maxOne = 0;
12
+ for (let i = 0; i < one; i++) {
13
14
+ maxOne++;
15
16
+ let max = maxOne;
17
+ for (let i = 1; i < nums.length; i++) {
18
+ if (nums[i - 1] == 1)
19
+ maxOne--;
20
+ if (nums[(i + one - 1) % nums.length] == 1)
21
22
+ if (maxOne > max)
23
+ max = maxOne;
24
25
+ return one - max;
26
+
27
+};
0 commit comments