leetcode 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold(python)

该篇文章讨论了如何在给定整数数组arr和两个整数k和threshold的情况下,计算满足平均值大于等于threshold的k长度子数组的数量。文章提供了一种优化的滑动窗口算法解决方案,以提高在LeetCode平台上的运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

Given an array of integers arr and two integers k and threshold.

Return the number of sub-arrays of size k and average greater than or equal to threshold.

Example 1:

Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
Output: 3
Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).

Example 2:

Input: arr = [1,1,1,1,1], k = 1, threshold = 0
Output: 5

Example 3:

Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
Output: 6
Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.

Example 4:

Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7
Output: 1

Example 5:

Input: arr = [4,4,4,4], k = 4, threshold = 1
Output: 1

Note:

1 <= arr.length <= 10^5
1 <= arr[i] <= 10^4
1 <= k <= arr.length
0 <= threshold <= 10^4

解析

根据题意,就是给出了一个整数列表 arr ,然后判断 arr 中的 k 个元素组成的子列表的平均数能够大于等于 threshold 的有多少个,最简单的方法就是暴力解法,直接从左到右遍历 k 元素所能组成的所有子列表,然后用计数器 count 统计平均值大于等于 threshold 的个数即可,但是运行结果是超时,因为题目中 arr 的长度最大可以达到 10^5 。

解答

class Solution(object):
    def numOfSubarrays(self, arr, k, threshold):
        """
        :type arr: List[int]
        :type k: int
        :type threshold: int
        :rtype: int
        """
        count = 0
        idx = 0
        while idx+k<=len(arr):
            tmp = arr[idx:idx+k]
            if sum(tmp)/len(tmp) >= threshold:
                count+=1
            idx += 1
        return count

运行结果

Time Limit Exceeded

解析

其实主要的耗时操作都在取自列表、求和、除法这三个关键的步骤上面,我们通过使用滑动窗口,来缩短计算时间,也就是每次只计算长度为 k 的窗口大小的和记录为 total ,向右滑动一个元素,计算新的窗口的和就是减去之前窗口的第一个元素且加上新进入窗口的最后一个元素,这样就可以大幅度减少求和的运算。

解答

class Solution(object):
    def numOfSubarrays(self, arr, k, threshold):
        """
        :type arr: List[int]
        :type k: int
        :type threshold: int
        :rtype: int
        """
        count = 0
        threshold = k*threshold
        total = sum(arr[:k])
        if total>=threshold:
            count += 1
        for i in range(k,len(arr)):
            total = total+arr[i]-arr[i-k]
            if total >= threshold:
                count += 1
        return count

运行结果

Runtime: 488 ms, faster than 100.00% of Python online submissions for Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold.
Memory Usage: 23.2 MB, less than 100.00% of Python online submissions for Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold.

原题链接:https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/

您的支持是我最大的动力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大丫丫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值