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 ...
随机推荐
- Linux系统查看主机性能
查看主机的CPU性能: cat /proc/cpuinfo cat /proc/meminfo |grep MemTotal 内存信息 查看物理cpu个数:cat /proc/cpuinfo ...
- el-select封装(单选框、多选框、全选功能)
先看看设计图: 网上找了一溜,都是扯淡,样式也没个 自己动手吧,先把样式搞定 popper-class="xx-option" 所有单选框都用 :after和:before类 + ...
- C++ STL之 map 学习笔记
•何为 map? map 是 STL 的一个关联容器,它提供一对一的数据处理,map 中存放的是一个 key-value键值对,其类型可以自己定义: 第一个可以称为关键字,每个关键字在 map 中只能 ...
- function 的入参 如果是指针的话,如果你用的好的话,会颠覆三观啊 这里就是指对象,数组不用考虑 // 夏娃的苹果
function 的入参 如果是指针的话,如果你用的好的话,会颠覆三观啊 这里就是指对象,数组不用考虑 这就是一颗 夏娃的苹果
- 表单验证 validate 两种 一种是callback配合外部变量,当同步用。第2中是 then async await 这种 真正$api也适用
validateFormRealProcessLeft () { let ret = false this.$refs.xxx.validate((valid) => { if (valid) ...
- CSharp的lambda表达式匿名类扩展方法
c#的lamba表达式 之前已经写过一些关于委托还有事件的文章,今天就来介绍一下lambda表达式. 首先定义需要的函数以及委托 { public delegate void DoNothingDel ...
- 16_AAC解码实战
本文主要讲解:如何将AAC编码后的数据解码成PCM. 命令行 用法非常简单: ffmpeg -c:a libfdk_aac -i in.aac -f s16le out.pcm -c:a libfdk ...
- FFmpeg命令行之ffmpeg
一.简述 ffmpeg是一个非常强大的工具,它可以转换任何格式的媒体文件,并且还可以用自己的AudioFilter以及VideoFilter进行处理和编辑.有了它,我们就可以对媒体文件做很多我们想做的 ...
- 人人都是艺术家!AI工具Doodly让潦草手绘变精美画作
AI绘画界太卷了,一天一个新东西,不久前刚给大家介绍了可以一秒出图的SDXL-Turbo,今天来聊一聊另一位重磅选手Doodly 有用过Stable Diffuison的小伙伴都知道,想要生成一张高质 ...
- ubuntu无法安装lrzsz
ubuntu无法安装lrzsz root@ubuntu:/opt/test# apt install lrzsz Reading package lists... Done Building depe ...