풀이
class Solution {
public int[] getAverages(int[] nums, int k) {
long[] prefix = new long[nums.length];
int[] answer = new int[nums.length];
prefix[0] = nums[0];
for(int i=1; i<nums.length; i++) {
prefix[i] = prefix[i-1] + nums[i];
}
for(int i=0; i<nums.length; i++) {
if(i-k>=0 && i+k<nums.length)
answer[i] = (int) ((prefix[i+k]-((i-k > 0)? prefix[i-k-1] : 0))/(2*k+1));
else
answer[i]=-1;
}
return answer;
}
}
오늘 Arrays and Strings 完!
'Software Engineering > Algorithm' 카테고리의 다른 글
[LeetCode] #560 Subarray Sum Equals K (0) | 2024.07.06 |
---|---|
[LeetCode] #1941 Check if All Characters Have Equal Number of Occurrences (0) | 2024.06.23 |
[LeetCode] #2248 Intersection of Multiple Arrays (0) | 2024.06.23 |
[LeetCode] First Letter to Appear Twice (1) | 2024.06.22 |
[LeetCode] Two Sum (0) | 2024.06.22 |