LeetCode 题解工作台
半径为 k 的子数组平均值
给你一个下标从 0 开始的数组 nums ,数组中有 n 个整数,另给你一个整数 k 。 半径为 k 的子数组平均值 是指: nums 中一个以下标 i 为 中心 且 半径 为 k 的子数组中所有元素的平均值,即下标在 i - k 和 i + k 范围( 含 i - k 和 i + k )内所有元素…
2
题型
5
代码语言
3
相关题
当前训练重点
中等 · 滑动窗口(状态滚动更新)
答案摘要
半径为 的子数组的长度为 $k \times 2 + 1$,因此我们可以维护一个大小为 $k \times 2 + 1$ 的窗口,记窗口中的所有元素和为 。 我们创建一个长度为 的答案数组 ,初始时每个元素都为 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 滑动窗口(状态滚动更新) 题型思路
题目描述
给你一个下标从 0 开始的数组 nums ,数组中有 n 个整数,另给你一个整数 k 。
半径为 k 的子数组平均值 是指:nums 中一个以下标 i 为 中心 且 半径 为 k 的子数组中所有元素的平均值,即下标在 i - k 和 i + k 范围(含 i - k 和 i + k)内所有元素的平均值。如果在下标 i 前或后不足 k 个元素,那么 半径为 k 的子数组平均值 是 -1 。
构建并返回一个长度为 n 的数组 avgs ,其中 avgs[i] 是以下标 i 为中心的子数组的 半径为 k 的子数组平均值 。
x 个元素的 平均值 是 x 个元素相加之和除以 x ,此时使用截断式 整数除法 ,即需要去掉结果的小数部分。
- 例如,四个元素
2、3、1和5的平均值是(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75,截断后得到2。
示例 1:

输入:nums = [7,4,3,9,1,8,5,2,6], k = 3 输出:[-1,-1,-1,5,4,4,-1,-1,-1] 解释: - avg[0]、avg[1] 和 avg[2] 是 -1 ,因为在这几个下标前的元素数量都不足 k 个。 - 中心为下标 3 且半径为 3 的子数组的元素总和是:7 + 4 + 3 + 9 + 1 + 8 + 5 = 37 。 使用截断式 整数除法,avg[3] = 37 / 7 = 5 。 - 中心为下标 4 的子数组,avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4 。 - 中心为下标 5 的子数组,avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4 。 - avg[6]、avg[7] 和 avg[8] 是 -1 ,因为在这几个下标后的元素数量都不足 k 个。
示例 2:
输入:nums = [100000], k = 0 输出:[100000] 解释: - 中心为下标 0 且半径 0 的子数组的元素总和是:100000 。 avg[0] = 100000 / 1 = 100000 。
示例 3:
输入:nums = [8], k = 100000 输出:[-1] 解释: - avg[0] 是 -1 ,因为在下标 0 前后的元素数量均不足 k 。
提示:
n == nums.length1 <= n <= 1050 <= nums[i], k <= 105
解题思路
方法一:滑动窗口
半径为 的子数组的长度为 ,因此我们可以维护一个大小为 的窗口,记窗口中的所有元素和为 。
我们创建一个长度为 的答案数组 ,初始时每个元素都为 。
接下来,我们遍历数组 ,将 的值加到窗口的和 中,如果此时 ,说明此时窗口大小为 ,那么 ,然后我们将 的值从窗口和 中移出。继续遍历下个元素。
最后返回答案数组即可。
时间复杂度 ,其中 为数组 的长度。忽略答案数组的空间消耗,空间复杂度 。
class Solution:
def getAverages(self, nums: List[int], k: int) -> List[int]:
n = len(nums)
ans = [-1] * n
s = 0
for i, x in enumerate(nums):
s += x
if i >= k * 2:
ans[i - k] = s // (k * 2 + 1)
s -= nums[i - k * 2]
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Test if the candidate can efficiently manage window updates for large inputs.
- question_mark
Evaluate if the candidate handles boundary conditions (less than k elements before or after index).
- question_mark
Assess the candidate's understanding of sliding window techniques and optimization.
常见陷阱
外企场景- error
Failing to correctly handle the boundary conditions when there are fewer than k elements before or after an index.
- error
Not maintaining an efficient running sum in the sliding window, leading to unnecessary recomputation of sums.
- error
Overlooking the time complexity and attempting a brute force solution that recalculates the sum for each subarray.
进阶变体
外企场景- arrow_right_alt
Generalize the solution to handle variable radius sizes.
- arrow_right_alt
Consider a variant where the input array contains negative numbers, testing if the candidate adapts the sliding window technique.
- arrow_right_alt
Modify the problem to compute a different statistic, such as the product or median, within the window.