K-Radius Subarray Averages
For each index, average the k values on each side of it, or return -1 when there are not enough.
The problem
Implement Solution.getAverages(nums, k). For every index i, look at the window from i - k to i + k, which has 2k + 1 values. If that window fits inside nums, the answer at i is the sum of the window divided by 2k + 1, rounded down; otherwise the answer at i is -1. Return the list of answers. For example, nums = [1, 2, 3, 4] with k = 1 gives [-1, 2, 3, -1]. nums has between 1 and 100,000 values, each from 0 to 100,000, and 0 <= k <= 100,000.
Starter code
class Solution:
def getAverages(self, nums, k):
passPublic examples
nums = [7, 4, 3, 9, 1, 8, 5, 2, 6], k = 3gives[-1, -1, -1, 5, 4, 4, -1, -1, -1]nums = [1, 2, 4], k = 1gives[-1, 2, -1]nums = [9, 3, 6], k = 1gives[-1, 6, -1]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
