LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I
643. Maximum Average Subarray I
题目描述
给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。
LeetCode643. Maximum Average Subarray I
示例 1:
输出: 12.75
解释: 最大平均数 (12-5-6+50)/4 = 51/4 = 12.75
注意:
Java 实现
class Solution {
public double findMaxAverage(int[] nums, int k) {
int sum = 0;
for (int i = 0; i < k; i++) {
sum += nums[i];
}
int max = sum;
for (int i = k; i < nums.length; i++) {
sum += nums[i] - nums[i - k];
max = Math.max(max, sum);
}
return (double) max / k;
}
}
相似题目
参考资料
- https://leetcode.com/problems/maximum-average-subarray-i/
- https://leetcode-cn.com/problems/maximum-average-subarray-i/
LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)的更多相关文章
- 【LeetCode】643. 子数组最大平均数 I Maximum Average Subarray I (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 目录 题目描述 题目大意 解题方法 方法一:preSum 方法二:滑动窗口 刷题心得 日期 题目地址:https://leetc ...
- [Swift]LeetCode643. 子数组最大平均数 I | Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- Java实现 LeetCode 643 子数组最大平均数 I(滑动窗口)
643. 子数组最大平均数 I 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.7 ...
- Leetcode 643.子数组最大平均数I
子数组最大平均数I 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.75 解释: ...
- 力扣643.子数组最大平均数I-C语言实现
题目 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例: 输入:[1,12,-5,-6,50,3], k = 4 输出:12.75 解释:最大平均数 (12-5- ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 【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 ...
随机推荐
- Mybatis 通用Mapper增强
1.确保是个Maven项目,确保Spring与Mybatis正确配置. 2.新建一个自定义通用Mapper. /** * BaseMapper接口:使mapper包含完整的CRUD方法<br&g ...
- Pytest权威教程01-安装及入门
目录 安装及入门 安装 Pytest 创建你的第一个测试用例 执行多条测试用例 断言抛出了指定异常 使用类组织多条测试用例 函数测试中请求使用独立的临时目录 进一步阅读 返回: Pytest权威教程 ...
- webbench 源代码
WebBench源码:https://github.com/EZLippi/WebBench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问 ...
- Complete the Projects
F1. Complete the Projects (easy version) F2. Complete the Projects (hard version) 参考:Complete the Pr ...
- @Autowired静态变量
@Component public class UserUtil{ private static Config config; @Autowired public void setConfig(Con ...
- 乌龙茶生产过程中挥发性成分吲哚的形成 | Formation of Volatile Tea Constituent Indole During the Oolong Tea Manufacturing Process
吲哚是啥?在茶叶成分中的地位?乌龙茶?香气,重要的前体,比如色氨酸Trp.IAA. Indole is a characteristic volatile constituent in oolong ...
- Windows环境下的安装gcc(c语言环境)
Windows 具有良好的界面和丰富的工具,所以目前 linux 开发的流程是, windows 下完成编码工作, linux 上实现编译工作. 为了提高工作效率,有必要在 windows 环境下搭建 ...
- nginx 80 下的一个路径 到 8888
# For more information on configuration, see:# * Official English Documentation: http://nginx.org/en ...
- etcd常用命令记录
etcd常用命令记录 1.查看etcd的版本 [root@etcd01 ssl]# curl -L http://127.0.0.1:2379/version {"etcdserver& ...
- C#多线程和异步——Task和async/await详解
阅读目录 一.什么是异步 二.Task介绍 1 Task创建和运行 2 Task的阻塞方法(Wait/WaitAll/WaitAny) 3 Task的延续操作(WhenAny/WhenAll/Cont ...