给定 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. DuiLib 一个window的皮肤库 C++ 项目,打包小,比较纯正主流的大制作

    https://github.com/duilib/duilib 从 火柴 那个软件 发现的这个库

  2. 关于wine乱码问题的解决方法

    在我的百度网盘里面,以及U盘备份,里面的Fonts.zip文件, 使用unzip Fonts.zip----linux指令 把这个文件夹里面的所有文件复制到wine的映射目录里面 cp Fonts/* ...

  3. HUAWEI WATCH GT3手表芯片传感器简析

    一 这里梳理一下华为手表GT3所使用的芯片 芯片A: BES2500L 恒玄BES2500L智能手表SoC,集存储.音频.连接为一体,集成BT5.2双模蓝牙,可支持BLE数据传输.蓝牙通话和音乐播放功 ...

  4. stm32L4xx串口日志配置解析

    前言: st这两年推出了一款超低功耗的芯片,stm32l4xx系列,该系列芯片有着功耗低,尺寸小等特点,非常适合应用在可穿戴式设备. 团队在这一领域深耕,所以不可避免的要用到这款芯片,这里就针对该芯片 ...

  5. ld: symbol(s) not found for architecture x86_64问题解决

    一 写在前面的话: 音频算法仿真过程中,本来是一个跑的好好地程序,突然间在mac下就报错了,出现的错误是: ld: symbol(s) not found for architecture x86_6 ...

  6. 05_QT_Mac开发环境搭建

    在不同的Mac环境下,实践出来的效果可能跟本教程会有所差异.我的Mac环境是:Intel CPU.macOS Moterey(12.4). FFmpeg 安装 在Mac环境中,直接使用Homebrew ...

  7. 提升UE5写实效果的项目设置

    随着虚幻引擎5(Unreal Engine 5,简称UE5)的发布,游戏开发者和数字艺术家们迎来了一个全新的机会,可以在其强大的渲染引擎下创建更加逼真和令人惊叹的游戏和虚拟场景.然而,要实现出色的写实 ...

  8. eviacam在Arch/Manjaro Linux下的安装

    安装base-devel 安装编译工具,默认的依赖里没有编译工具 sudo yay -S base-devel 如果安装编译工具,会报类似下面的错误: 安装eviacam yay -S eviacam ...

  9. 记录--css水滴登录界面

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 今天我们来分享一款非常有趣的登录界面,它使用HTML和CSS制作,具有动态的水波纹效果,让用户在登录时感受到了一股清凉之感. 基本h ...

  10. 记录--用JS轻松实现一个录音、录像、录屏的工具库

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 最近项目遇到一个要在网页上录音的需求,在一波搜索后,发现了 react-media-recorder 这个库.今天就跟大家一起研究一 ...