-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
35 lines (26 loc) · 746 Bytes
/
Copy pathsolution.java
File metadata and controls
35 lines (26 loc) · 746 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
30
31
32
33
34
35
class Solution {
int minToggle(int[] arr) {
// Count total zeros in the array
int rightZeros = 0;
for (int num : arr) {
if (num == 0)
rightZeros++;
}
// Initial answer
int ans = rightZeros;
// Count ones on left side
int leftOnes = 0;
// Try every partition
for (int num : arr) {
// Remove zero from right side
if (num == 0)
rightZeros--;
// Add one to left side
else
leftOnes++;
// Update minimum toggles
ans = Math.min(ans, leftOnes + rightZeros);
}
return ans;
}
}