-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_1151.java
More file actions
29 lines (21 loc) · 802 Bytes
/
Copy pathleetCode_1151.java
File metadata and controls
29 lines (21 loc) · 802 Bytes
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
class Solution {
public int minSwaps(int[] data) {
int[] num_of_ones= new int[data.length];
num_of_ones[0] = data[0];
int total_ones = data[0] == 1 ? 1 : 0;
for(int i=1; i < data.length; i++){
num_of_ones[i] = num_of_ones[i-1];
if(data[i] == 1){
num_of_ones[i]++;
total_ones++;
}
}
if(total_ones == 0) return 0;
int max_ones = num_of_ones[total_ones-1];
for(int i=total_ones ; i < data.length; i++){
int ones_till_here = num_of_ones[i] - num_of_ones[i - total_ones] ;
max_ones = Math.max(max_ones, ones_till_here);
}
return total_ones - max_ones;
}
}