给定 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. 你想要一个简单的 MQ 吗?(最简单的那种)

    FolkMQ 一个简单的消息中间件(全球最简单的那种,要比谁都简单!).追世间简单为何物,可叫我生死相许! 面向简单编程 1) 启动服务 docker run -p 18602:18602 -p 86 ...

  2. 【规范】看看人家Git提交描述,那叫一个规矩

    前言 缘由 没想到玩了多年git,竟然还有提交描述规范 事情起因: 在工作迭代过程中,偶然发现同组小帅哥Git提交描述总是和自己的不大一样,秉承好奇至上的我特意去研究了下.竟然发现提交了这么多年的Gi ...

  3. [【stars-one】Android图标生成器 PC工具

    原文: [stars-one]Android图标生成器 - Stars-One的杂货小窝 一款Android开发者的PC工具软件,可以快速生成android开发需要使用的xml矢量图标文件 起因 个人 ...

  4. Android Studio导出APP的数据库db文件

    原文地址:Android Studio导出APP的数据库db文件 | Stars-One的杂货小窝 最近项目开发需要使用到Android内置的Sqlite存数据,但是公司里没有对应的调试环境,只能让现 ...

  5. Ubuntu下安装Android Studio

    一.系统环境 二.安装源文件 Android Studio 4.2.2:android-studio-ide-202.7486908-linux.tar.gz Java SE Development ...

  6. 编译器与Makefile

    编译器与Makefile 目录 编译器与Makefile gcc/g++/clang clang gcc g++ 编译器过程 Makefile 什么是Makefile Makefile规则 变量 in ...

  7. 多线程系列(二十一) -ForkJoin使用详解

    一.摘要 从 JDK 1.7 开始,引入了一种新的 Fork/Join 线程池框架,它可以把一个大任务拆成多个小任务并行执行,最后汇总执行结果. 比如当前要计算一个数组的和,最简单的办法就是用一个循环 ...

  8. 02.Android崩溃Crash库之App崩溃分析

    目录总结 01.抛出异常导致崩溃分析 02.RuntimeInit类分析 03.Looper停止App就退出吗 04.handleApplicationCrash 05.native_crash如何监 ...

  9. 记一次 .NET某游戏后端API服务 CPU爆高分析

    一:背景 1. 讲故事 前几天有位朋友找到我,说他们的API服务程序跑着跑着CPU满了降不下去,让我帮忙看下怎么回事,现在貌似民间只有我一个人专注dump分析,还是申明一下我dump分析是免费的,如果 ...

  10. 详解SSL证书系列(7)HTTP的三大缺点

    我们已经了解到HTTP协议具有相当优秀和方便的一面,然而HTTP并非只有好的一面,事物皆具有两面性,它也是有不足之处的,那么HTTP有哪些缺点呢? 窃听风险 由于HTTP本身不具备加密的功能,所以也无 ...