forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
28 lines (28 loc) · 849 Bytes
/
Solution.java
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
class Solution {
public int minMoves(int[] nums, int k) {
List<Integer> arr = new ArrayList<>();
int n = nums.length;
for (int i = 0; i < n; ++i) {
if (nums[i] != 0) {
arr.add(i);
}
}
int m = arr.size();
int[] s = new int[m + 1];
for (int i = 0; i < m; ++i) {
s[i + 1] = s[i] + arr.get(i);
}
long ans = 1 << 60;
int x = (k + 1) / 2;
int y = k - x;
for (int i = x - 1; i < m - y; ++i) {
int j = arr.get(i);
int ls = s[i + 1] - s[i + 1 - x];
int rs = s[i + 1 + y] - s[i + 1];
long a = (j + j - x + 1L) * x / 2 - ls;
long b = rs - (j + 1L + j + y) * y / 2;
ans = Math.min(ans, a + b);
}
return (int) ans;
}
}