forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2090.java
31 lines (30 loc) · 1 KB
/
_2090.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
29
30
31
package com.fishercoder.solutions;
public class _2090 {
public static class Solution1 {
public int[] getAverages(int[] nums, int k) {
if (k == 0) {
return nums;
}
long[] preSums = new long[nums.length];
preSums[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
preSums[i] = preSums[i - 1] + nums[i];
}
int[] ans = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
if (i - k < 0) {
ans[i] = -1;
} else if (i + k >= nums.length) {
ans[i] = -1;
} else {
if (i - k == 0) {
ans[i] = (int) (preSums[i + k] / (2 * k + 1));
} else {
ans[i] = (int) ((preSums[i + k] - preSums[i - k - 1]) / (2 * k + 1));
}
}
}
return ans;
}
}
}