Rust 力扣 - 1343. 大小为 K 且平均值大于等于阈值的子数组数目

文章目录

题目描述

题解思路

长度为k且平均值大于等于阈值的子数组数目 等于 长度为k且总和大于等于k * 阈值的子数组数目

我们遍历长度为k的窗口,我们只需要记录窗口内的总和即可,遍历过程中记录总和大于等于k * 阈值的子数组数目

题解代码

rust 复制代码
impl Solution {
    pub fn num_of_subarrays(arr: Vec<i32>, k: i32, threshold: i32) -> i32 {    
        let threshold = k * threshold;

        let mut ans = 0;
        let mut sum = 0;

        for i in 0..k as usize {
            sum += arr[i];
        }

        if sum >= threshold {
            ans += 1;
        }

        for i in k as usize..arr.len() {
            sum += arr[i] - arr[i - k as usize];
            if sum >= threshold {
                ans += 1;
            }
        }

        ans
    }
}

题目链接

https://leetcode.cn/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/

相关推荐
帅那个帅1 小时前
PHP里面的抽象类和接口类
开发语言·php
咖啡の猫7 小时前
Python字典推导式
开发语言·python
leiming67 小时前
C++ vector容器
开发语言·c++·算法
掘金码甲哥7 小时前
🚀糟糕,我实现的k8s informer好像是依托答辩
后端
SystickInt8 小时前
C语言 strcpy和memcpy 异同/区别
c语言·开发语言
GoGeekBaird8 小时前
Andrej Karpathy:2025年大模型发展总结
后端·github
CS Beginner8 小时前
【C语言】windows下编译mingw版本的glew库
c语言·开发语言·windows
uzong8 小时前
听一听技术面试官的心路历程:他们也会有瓶颈,也会表现不如人意
后端
Jimmy8 小时前
年终总结 - 2025 故事集
前端·后端·程序员
FJW0208148 小时前
Python_work4
开发语言·python