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/

相关推荐
happymaker06266 分钟前
LeetCodeHot100——155.最小栈
算法
xingyuzhisuan7 分钟前
缓存命中率提升方案:从 30% 优化至 82% 全流程优化记录
java·开发语言·缓存·ai
郑洁文13 分钟前
基于Python的恶意流量监测系统的设计与实现
开发语言·python
AI玫瑰助手15 分钟前
Python流程控制:for循环与range函数的搭配使用
开发语言·python·信息可视化
guyoung16 分钟前
BoxAgnts 工具系统(6)——多 Provider 适配与 Agent 查询循环
rust·agent·ai编程
洛水水16 分钟前
【力扣100题】85.每日温度
算法·leetcode·职场和发展
一条泥憨鱼17 分钟前
Java开发效率神器:Lombok从入门到精通!
java·后端·学习·开发·lombok
anew___18 分钟前
2026年Python爬虫技术完全指南:从入门到实战
开发语言·爬虫·python
熠熠仔19 分钟前
Spring Boot 与 MyBatis-Plus 空间几何数据集成指南
spring boot·后端·mybatis
Penfy_Z19 分钟前
【Python LLM 调用踩坑】Connection error 终极解决方案!npm 代理导致阿里云通义千问接口连接失败
开发语言·python·npm