当前位置:网站首页>LeetCode_643_子数组的最大平均数Ⅰ
LeetCode_643_子数组的最大平均数Ⅰ
2022-08-04 12:46:00 【Fitz1318】
题目链接
题目描述
给你一个由 n 个元素组成的整数数组 nums 和一个整数 k 。
请你找出平均数最大且 长度为 k 的连续子数组,并输出该最大平均数。
任何误差小于 1 0 − 5 10^{-5} 10−5 的答案都将被视为正确答案。
示例 1:
输入:nums = [1,12,-5,-6,50,3], k = 4
输出:12.75
解释:最大平均数 (12-5-6+50)/4 = 51/4 = 12.75
示例 2:
输入:nums = [5], k = 1
输出:5.00000
提示:
n == nums.length1 <= k <= n <= 10^5-10^4 <= nums[i] <= 10^4
解题思路
滑动窗口
滑动窗口,记录每k个数的和,总和最大的平均值也就最大
AC代码
class Solution {
public double findMaxAverage(int[] nums, int k) {
int left = 0;
int right = k;
int sum = 0;
for (left = 0; left < right; left++) {
sum += nums[left];
}
int maxSum = sum;
for (left = 0, right = k; right < nums.length; left++, right++) {
sum = sum - nums[left] + nums[right];
maxSum = Math.max(sum, maxSum);
}
return 1.0 * maxSum / k;
}
}
边栏推荐
- 永磁同步电机FOC驱动代码讲解
- Two years of independent development experience Programmers tell us the experience of making money (listen to the masters who really make money)
- Control CD-ROM with VbScript
- Ultra-QuickSort
- 接到“网站动态换主题”的需求,我是如何踩坑的
- Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
- 移动跨端技术方案分析对比
- Valentine's Day Romantic 3D Photo Wall [with source code]
- num_workers
- ShanDong Multi-University Training #4 A、B、C、G
猜你喜欢

Ceres库运行,模板内报内存冲突问题。(已解决)

七夕疯狂搞钱的年轻人,一周赚14万

持续交付(三)Jenkinsfile语法使用介绍

酷开科技 × StarRocks:统一 OLAP 分析引擎,全面打造数字化的 OTT 模式
![Valentine's Day Romantic 3D Photo Wall [with source code]](/img/a9/2c26f4f048f3c0a9a65551bc734233.png)
Valentine's Day Romantic 3D Photo Wall [with source code]

rpm安装提示error: XXX: not an rpm package (or package manifest):

集群监控——Zabbix

"Lonely Walking on the Moon" is a powerful medicine, it can't cure the internal friction of happy twist

为什么密码云服务平台是云时代的必然之选?

The head module of the yolo series
随机推荐
Ultra-QuickSort
面试官:连 INSERT INTO SET 都不知道怎么用,你这3年都干些什么了?
缓存字符流
redis未授权访问漏洞【vulhub靶场】复现
小程序在政务服务平台建设中如何发挥价值
两个数组中用第二个数组的Value对比换第一个数组中的Key
【解决方案 三十一】Navicat数据库结构同步
Oracle 19c 单实例 19.3.0 升级到19.11.0 详细教程
持续交付(三)Jenkinsfile语法使用介绍
Cool and efficient data visualization big screen, it's really not that difficult to do!丨Geek Planet
数据中台建设(九):数据中台资产运营机制
持续交付(二)PipeLine基本使用
情人节浪漫3D照片墙【附源码】
d不要直接用转串
LeetCode Daily Question (858. Mirror Reflection)
Arduino框架下I2S控制ADC采样以及PWM输出示例解析
用VbScript控制光驱
常用代码模板1——基础语法
Cache character stream
Motion Rule (16)-Union Check Basic Questions-Relations