给定 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 - 子数组最大平均值的更多相关文章

  1. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  2. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  3. [LeetCode] Maximum Average Subarray I 子数组的最大平均值

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  4. 【LeetCode】643. 子数组最大平均数 I Maximum Average Subarray I (Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 目录 题目描述 题目大意 解题方法 方法一:preSum 方法二:滑动窗口 刷题心得 日期 题目地址:https://leetc ...

  5. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. 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 ...

  7. [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 ...

  8. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  9. [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 ...

  10. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

随机推荐

  1. .NET 5 以后的 HttpClient 超时问题

    背景 起因是朋友在使用深信服的 Easy Connect 连接到内网之后,使用 HttpClient 访问对应内网的 API 站点均返回 System.Net.Sockets.SocketExcept ...

  2. chatGPT初体验

    chatGPT NLP技术,通过统计的手段模拟出更正确的答案. 他与以前的NLP不一样,他有上下文语义,他能够模拟场景,能够总结很多文章信息. 因此对于谷歌等搜索引擎就很有攻击性了,因为chatGPT ...

  3. 尚硅谷Java 宋红康2023版 - 学习笔记

    尚硅谷Java 宋红康2023版 - 学习笔记 观看地址 https://www.bilibili.com/video/BV1PY411e7J6 60-IDEA开发工具-HelloWorld的编写与相 ...

  4. Electron 开发过程中主进程的无法看到 console.log 输出怎么办

    开发过程中命令行工具(powershell.terminal)内无法看到 console.log 输出 Eelectron 的在开发过程中主进程 NodeJS 内往往需要 console.log 来进 ...

  5. 记一次配置mybatis plus报错有感

    参考,欢迎点击原文:https://blog.csdn.net/wwrzyy/article/details/86034458(问题原因) https://www.jianshu.com/p/28d6 ...

  6. ubuntu20 宽带连接

    nmcli connection edit type pppoe con-name "连接名称" set pppoe.username 宽带账号 set pppoe.passwor ...

  7. 纯前端实现 PNG 图片压缩 | UPNG.js

    在线 Demo 体验地址 →: https://demos.sugarat.top/pages/png-compress/ 前言 最近在迭代自己的 图床 应用,由于使用时间的累计,存储空间占用越来越大 ...

  8. 记录--再也不用手动改package.json的版本号

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 本文的起因是有在代码仓库发包后,同事问我"为什么package.json 里的版本还是原来的,有没有更新?",这个时候 ...

  9. 记录--两行CSS让页面提升了近7倍渲染性能!

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 对于前端人员来讲,最令人头疼的应该就是页面性能了,当用户在访问一个页面时,总是希望它能够快速呈现在眼前并且是可交互状态.如果页面加载 ...

  10. C#进阶篇

    ArrayList 1.Arraylist本质上是一个可以自动扩容的object数组 2.由于用万物之父来存储数据,自然存在装箱拆箱 3.当往其中进行值类型存储时就是在装箱,当将值类型对象取出来转换使 ...