leetcode - 子数组最大平均值
给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。
示例:
输入:[1,12,-5,-6,50,3], k = 4
输出:12.75
解释:最大平均数 (12-5-6+50)/4 = 51/4 = 12.75
提示:
1 <= k <= n <= 30,000。
-所给数据范围 [-10,000,10,000]
public class Main {
public static void main(String[] args) {
int[] in = {1, 12, -5, -6, 50, 3};
int k = 4;
int sum = 0;
int n = in.length;
// 求出初始K个数组大小的总数
for (int i = 0; i < k; i++) {
sum = sum + in[i];
}
int maxSum = sum;
// 遍历从K开始知道最大值之间的值,求出总体数组中最大的值
for (int i = k; i < n; i++) {
//减去最小值,加上最大值,来回和sum值比较,用来更新max值
sum = sum - in[i - k] + in[i];
// 比较获取最大值
maxSum = Math.max(maxSum, sum);
}
float averageValue = maxSum * 1.0F / k;
System.out.println(averageValue);
}
}
leetcode - 子数组最大平均值的更多相关文章
- [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] Maximum Average Subarray I 子数组的最大平均值
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 【LeetCode】643. 子数组最大平均数 I Maximum Average Subarray I (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 目录 题目描述 题目大意 解题方法 方法一:preSum 方法二:滑动窗口 刷题心得 日期 题目地址:https://leetc ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
随机推荐
- 学习ASP.NET Core Razor 编程系列文章目录
学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二--添加一个实体 学习ASP.NET Core Razor 编程系列三--创建数据表及创建项目 ...
- .NET Conf China 2023济南站社区活动
2024年3月3日,在这个春暖花开的日子里,由微软MVP项目.山东财经大学管理科学与工程学院.胶东开发者社区.济南.NET俱乐部联合举办了[.NET Conf China 2023 JiNan Wa ...
- OBS Studio多开/多平台直播的最佳解决方案
OBS是一款强大的PC端免费的直播推流及录制工具,功能很强大,它支持开发者根据需求开发插件集成使用 我们知道OBS支持通过插件来实现多路推流,它的优点是只需开启一个OBS窗口就可以同时推流到多个不同的 ...
- [VueJsDev] 日志 - BBTime-LOG
[VueJsDev] 目录列表 https://www.cnblogs.com/pengchenggang/p/17037320.html BBTime-LOG ::: details 目录 目录 B ...
- 软件推荐 Notable / 现改用 Vnote 了
https://notable.app/#download
- 基于python的固定间隔时间执行实例解析
一 概念 datetime的用法如下: import datetime. # 打印当前时间 time1 = datetime.datetime.now() print(time1) # 打印按指定格式 ...
- CMake的作用和价值--概念简介
一 简介: CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似 ...
- 在后台运行 django的基本方法
在后台运行 django: nohup python manage.py runserver 0.0.0.0:9000 &ps:&可以不写,这样启动测试服务器后,就可以常驻后台运行了. ...
- Excalidraw:绘制图形的新利器
摘要: Excalidraw是一款简洁设计.直观易用的绘图应用,用户可以通过它创建流程图.示意图.架构图等各种图形.除了提供手绘效果外,Excalidraw还支持多人实时协作编辑,并提供端到端加密以确 ...
- .net core WPF关于进程打开网页链接报错"文件找不到"
System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = " ...