给定 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. redis---面经

    redis 偏应用的总结:redis 应用 Redis是什么? Redis是什么 对象 字符串 自增,键值对. SDS数据结构记录长度,已经使用,和总共长度,并且提前多余出容量,防止一直扩容缩容. 字 ...

  2. axios post xml data方法

    axios#request(config) axios#get(url[,config]) axios#delete(url[,config]) axios#head(url[,config]) ax ...

  3. 火柴 基于everything的搜索软件 软件推荐 Ctrl+Ctrl 显示 tab转换 本机搜索和网络搜索

    https://www.huochaipro.com/

  4. C# 常用特性(Attribute)

    目录 指定方法实现的属性MethodImplAttribute 标记不再使用的程序元素ObsoleteAttribute 指定属性或事件的说明DescriptionAttribute 未完待续... ...

  5. 【大语言模型基础】-详解Transformer原理

    一.Transformer Transformer最开始用于机器翻译任务,其架构是seq2seq的编码器解码器架构.其核心是自注意力机制: 每个输入都可以看到全局信息,从而缓解RNN的长期依赖问题. ...

  6. FreeRTOS教程4 消息队列

    1.准备材料 正点原子stm32f407探索者开发板V2.4 STM32CubeMX软件(Version 6.10.0) Keil µVision5 IDE(MDK-Arm) 野火DAP仿真器 XCO ...

  7. Spring boot返回时间与MySql数据库中不相同问题及解决方法

    最近做项目测试的发现,访问Url返回的时间与数据库中的不相同,环境是Spring boot+MyBatis+Mysql(阿里云服务器),经过一番折腾,得到了解决 问题描述 我是直接使用IDEA的数据库 ...

  8. epoll和ractor的粗浅理解

    我们继续上篇的文章继续更新我们的代码. 首先就是介绍一下epoll的三个函数. epoll_create epoll_ctl epoll_wait 如何去理解这3个函数,我是这样去理解这个函数, 就像 ...

  9. API和String字符串介绍

    API 1.如何使用Java已经写好的东西(方法,类) API(Application programming interface):应用程序编程接口 简单理解:API就是别人已经写好了的东西,我们不 ...

  10. vector的Erase相关

    vector<int>Vect; Vect.insert(Vect.begin()+2, 50); for (auto it=Vect.begin();it!=Vect.end();++i ...