给定 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. autohotkey 设置快捷键 设置光标位置 (ctrl + alt + Numpad0)

    autohotkey 设置快捷键 设置光标位置 (ctrl + alt + Numpad0) 原因 3个屏幕,所以鼠标设置的灵敏度非常高,经常就找不到鼠标在哪了. 设置个快捷键,让鼠标每次都初始化一个 ...

  2. 英语字母z解析.drawio

    英语字母z解析.drawio

  3. JB一键重置

    版本名 版本号 更新时间 更新内容 更新地址 v1.1 2 2020-12-26 在线下载失败的请访问shop.stars-one.site,使用软件找回手动下载\n1.修复bug\n2.修复在线更新 ...

  4. 17_详解YUV

    本文的主角是多媒体领域非常重要的一个概念:YUV. 简介 YUV,是一种颜色编码方法,跟RGB是同一个级别的概念,广泛应用于多媒体领域中. 也就是说,图像中每1个像素的颜色信息,除了可以用RGB的方式 ...

  5. 毕设系列之JrtpLib H264(裸视频数据) 实时视频传输(发送与接受)

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  6. spring 整体框架介绍

    一.什么是Spring? 二.Spring能够解决什么问题? 三.Spring整体架构

  7. Java SE 22 新增特性

    Java SE 22 新增特性 作者:Grey 原文地址: 博客园:Java SE 22 新增特性 CSDN:Java SE 22 新增特性 源码 源仓库: Github:java_new_featu ...

  8. cadence板图设计基本操作

    基于cadence的四位全加器设计及仿真. 1.实验原理 板图,也就是芯片的原理图.通过学习板图的绘制,可以有效地提高对芯片的工作原理的认识.在版图设计中,需要掌握许多的规则,能够按照特定的规范优化, ...

  9. KingbaseES V8R6 集群运维案例 -- 磁盘空间问题导致集群故障

    某商业银行生产系统KingbaseES读写分离集群主库出现故障,导致集群主备发生切换.客户要求说明具体的原因. KingbaseES读写分离集群基本信息: KingbaseES集群信息   操作系统 ...

  10. Java面试题【3】

    20)什么是线程安全? 含义:当多个线程访问某个方法时,不管你通过怎样的调用方式或者说这些线程如何交替的执行,我们在主程序中不需要去做任何的同步,这个类的结果行为都是我们设想的正确行为,那么我们就可以 ...