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/

相关推荐
Sherotree13 分钟前
Google Antigravity——Agent 被提到主界面
前端·ide·后端·edge浏览器
鹿角片ljp24 分钟前
LeetCode 236. 二叉树的最近公共祖先|递归后序
算法
2601_9620562333 分钟前
Spring Boot 入门 与 无法解析符号 springframework 的解决
java·spring boot·后端
luj_176836 分钟前
大律师考核应重能力与科技素养
c语言·开发语言·c++·经验分享·算法
无定义_41 分钟前
Floyd——Warshall
算法
刃神太酷啦1 小时前
Linux 系统 MySQL 完整安装配置教程:从卸载 MariaDB 到优化 my.cnf----《Hello MySQL!》(1)
android·linux·c语言·c++·mysql·leetcode·mariadb
你驴我1 小时前
WhatsApp 多账号场景下的会话归档与历史消息检索优化实践
后端·python
学长毕业设计2 小时前
基于SpringBoot的瑜伽馆网站的设计与实现(源码+文档+讲解视频)
java·spring boot·后端
带多刺的玫瑰2 小时前
Leecode#9刷题之回文数
数据结构·算法
leonkay2 小时前
C# 特性(Attribute)——【1】基础讲解
开发语言·青少年编程·面试·c#·.net·个人开发