2090. K Radius Subarray Averages
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 46% Topics: Array, Sliding Window
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def getAverages(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
total, l = 0, 2*k+1
result = [-1]*len(nums)
for i in xrange(len(nums)):
total += nums[i]
if i-l >= 0:
total -= nums[i-l]
if i >= l-1:
result[i-k] = total//l
return result
Solution from kamyu104/LeetCode-Solutions · MIT