643. Maximum Average Subarray I
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
double findMaxAverage(vector<int>& nums, int k)
{
int sz=nums.size();
double maxsum=0.0;
double cursum=0.0;
for(int i=;i<k;i++)
cursum+=nums[i];
maxsum=cursum;
for(int j=k;j<sz;j++)
{
cursum=cursum-nums[j-k]+nums[j];
maxsum=max(cursum,maxsum);
}
return maxsum/k;
}
};
扫描一遍,更新当前和,最大和,最后取走最大和即可
643. Maximum Average Subarray I的更多相关文章
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 643. Maximum Average Subarray
Given an array consisting of \(n\) integers, find the contiguous subarray of given length \(k\) that ...
- [Leetcode]643. Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- leetcode 643. Maximum Average Subarray I 子数组最大平均数 I
一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...
- LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
随机推荐
- 18. 进livebos对象直接跳转jsp页面的做法
在网格脚本定义添加: window.onload=function(){ window.location.href='/plug-in/sinopec/contractManagement/h ...
- spring boot 整合 (全)
参考: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
- VBA 选择文件
Private Function SelectFile(ByVal strFilter As String) As String Dim FileName As Variant '打开文 ...
- 使用plsql进行查询的时候出现错误:动态执行表不可访问,本会话的自动统计被终止
- WP8.1 发送邮件
Method 1: Windows.System.Launcher.LaunchUriAsync(new Uri("abc@outlook.com?subject=hello world&a ...
- kafka 修改partition,删除topic,查询offset
修改分区个数: ./kafka-topics./kafka/<id_of_kafka> --alter --partitions 10 --topic test_topic 上面命令将te ...
- python 日期格式
%a 星期几的简写%A 星期几的全称%b 月分的简写%B 月份的全称%c 标准的日期的时间串%C 年份的后两位数字%d 十进制表示的每月的第几天%D 月/天/年%e 在两字符域中,十进制表示的每月的第 ...
- 大型运输行业实战_day08_1_memcache缓存生产应用
1.memcache使用环境搭建 1.安装memcached服务器 安装方法 以管理员身份打开cmd,在cmd中执行如下命令: 注意:在执行该命令时必须在memcached.exe文件下执行. 2.开 ...
- Java8 Stream语法详解 2
1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel agg ...
- select top 变量问题
1.拼接查询语句(SQL2000,2005,2008均可) DECLARE @a AS INT SET @a=1 EXEC('SELECT TOP '+@a+' * FROM mtrcLanguage ...