@@ -4,44 +4,44 @@ class Solution {
4
4
int n = nums.size ();
5
5
vector<int > cnt (n + 1 , 0 );
6
6
vector<long long > s (n + 1 , 0 );
7
-
7
+
8
8
for (int i = 1 ; i <= n; ++i) {
9
9
cnt[i] = cnt[i - 1 ] + nums[i - 1 ];
10
10
s[i] = s[i - 1 ] + 1LL * i * nums[i - 1 ];
11
11
}
12
-
12
+
13
13
long long ans = LLONG_MAX;
14
-
14
+
15
15
for (int i = 1 ; i <= n; ++i) {
16
16
long long t = 0 ;
17
17
int need = k - nums[i - 1 ];
18
-
18
+
19
19
for (int j = i - 1 ; j <= i + 1 ; j += 2 ) {
20
20
if (need > 0 && 1 <= j && j <= n && nums[j - 1 ] == 1 ) {
21
21
--need;
22
22
++t;
23
23
}
24
24
}
25
-
25
+
26
26
int c = min (need, maxChanges);
27
27
need -= c;
28
28
t += c * 2 ;
29
-
29
+
30
30
if (need <= 0 ) {
31
31
ans = min (ans, t);
32
32
continue ;
33
33
}
34
-
34
+
35
35
int l = 2 , r = max (i - 1 , n - i);
36
-
36
+
37
37
while (l <= r) {
38
38
int mid = (l + r) / 2 ;
39
39
int l1 = max (1 , i - mid), r1 = max (0 , i - 2 );
40
40
int l2 = min (n + 1 , i + 2 ), r2 = min (n, i + mid);
41
-
41
+
42
42
int c1 = cnt[r1] - cnt[l1 - 1 ];
43
43
int c2 = cnt[r2] - cnt[l2 - 1 ];
44
-
44
+
45
45
if (c1 + c2 >= need) {
46
46
long long t1 = 1LL * c1 * i - (s[r1] - s[l1 - 1 ]);
47
47
long long t2 = s[r2] - s[l2 - 1 ] - 1LL * c2 * i;
@@ -52,7 +52,7 @@ class Solution {
52
52
}
53
53
}
54
54
}
55
-
55
+
56
56
return ans;
57
57
}
58
58
};
0 commit comments