Skip to content

Commit 7aae3ab

Browse files
committed
Runtime: 229 ms (Top 10.78%) | Memory: 80.5 MB (Top 44.03%)
1 parent aa5575d commit 7aae3ab

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

scripts/algorithms/M/Minimum Swaps to Group All 1's Together II/Minimum Swaps to Group All 1's Together II.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 229 ms (Top 10.78%) | Memory: 80.5 MB (Top 44.03%)
12
class Solution {
23
public:
34
int minSwaps(vector<int>& nums) {
@@ -7,9 +8,9 @@ class Solution {
78
if(nums[i]==1) ones++;
89
}
910
if(ones<=1) return 0;
10-
11+
1112
int k = ones; // sliding window length
12-
int maxOnes = 0; // maxOnes in sub array of length k
13+
int maxOnes = 0; // maxOnes in sub array of length k
1314
int cnt = 0;
1415
for(int i=0;i<k;i++){
1516
if(nums[i]==1) cnt++;
@@ -20,6 +21,6 @@ class Solution {
2021
if(nums[i%n]==1) cnt++; // if element adding to window is 1, then increase count.
2122
maxOnes = max(maxOnes,cnt); // maintaing maxOnes for all sub arrays of length k.
2223
}
23-
return(k - maxOnes); // (total length of subarray - ones in the sub array found)
24+
return(k - maxOnes); // (total length of subarray - ones in the sub array found)
2425
}
25-
};
26+
};

0 commit comments

Comments
 (0)