一、题目大意

https://leetcode.cn/problems/maximum-average-subarray-i/

给你一个由 n 个元素组成的整数数组 nums 和一个整数 k 。

请你找出平均数最大且 长度为 k 的连续子数组,并输出该最大平均数。

任何误差小于10^(-5)的答案都将被视为正确答案。

示例 1:

输入:nums = [1,12,-5,-6,50,3], k = 4

输出:12.75

解释:最大平均数 (12-5-6+50)/4 = 51/4 = 12.75

示例 2:

输入:nums = [5], k = 1

输出:5.00000

提示:

  • n == nums.length
  • 1 <= k <= n <= 105
  • -104 <= nums[i] <= 104

二、解题思路

滑动窗口的思路解决,窗口长度固定一直向右滑动即可

三、解题方法

3.1 Java实现

public class Solution {
public double findMaxAverage(int[] nums, int k) {
// 初始化窗口
double total = 0;
for (int i = 0; i < k; i++) {
total += nums[i];
}
double maxAvg = total / k;
// 向右移
for (int right = k; right < nums.length; right++) {
total = total + nums[right] - nums[right - k];
double tmpAvg = total / k;
if (tmpAvg >= maxAvg) {
maxAvg = tmpAvg;
}
}
return maxAvg;
}
}

四、总结小记

  • 2022/5/17 了解了滑动窗口的思想,解决这道题就很简单了

leetcode 643. Maximum Average Subarray I 子数组最大平均数 I的更多相关文章

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

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

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

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

  3. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

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

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

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

  5. [Leetcode]643. Maximum Average Subarray I

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

  6. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

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

  7. Leetcode643.Maximum Average Subarray I子数组的最大平均数1

    给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.75 解释: 最大平均数 (12- ...

  8. 【Leetcode_easy】643. Maximum Average Subarray I

    problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...

  9. 643. Maximum Average Subarray I 最大子数组的平均值

    [抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...

随机推荐

  1. STM32 中的 assert_param 函数

    在学STM32的时候函数assert_param出现的几率非常大,上网搜索一下,网上一般解释断言机制,做为程序开发调试阶段时使用. 断言机制函数assert_param我们在分析库函数的时候,几乎每一 ...

  2. 制作任意波形发生器MAX038

  3. H5新增API

    H5新增API 选择器 querySelector()和querySelectorAll(),参数都是css选择器,前者返回符合条件的第一个匹配的元素,如果没有则返回Null,后者返回符合筛选条件的所 ...

  4. python爬取梦幻西游召唤兽资质信息(不包含变异)

    一.分析 1.爬取网站:https://xyq.163.com/chongwu/ 2.获取网页源码: request.get("https://xyq.163.com/chongwu/&qu ...

  5. 体温填报app作业演示

    今日学习 今天把这个体温填写app作业,做完了. 具体开发流程:https://www.cnblogs.com/yuxuan-light-of-Taihu-Lake/p/14362107.html 点 ...

  6. HTML2Canvas使用总结

    1:指定要生成的DOM元素id 2: 某些图片动态赋值src的url或者base64可能不会被立即渲染:可以设置一个定时器解决 3:可以调用次方法得到的canvas元素转一下格式 png/jpg 4: ...

  7. python---100以内所有素数

    def get_primes(): """ 100以内的所有素数:每个数都对从2到其本身前一个数做整除, 遇到能整除就换下一个数. 如果从2到去本身前一个数都没有整除,则 ...

  8. python的注释、变量、常量基础

    一.注释 1.什么是注释 注释就是对代码的解释说明,注释的内容不会被当作代码运行 2.为什么要注释 增强代码的可读性 3.怎么用注释? 代码注释单行和多行注释 单行注释用#号,可以跟在代码的正上方或正 ...

  9. 3.Docker容器学习之新手基础使用

    原文地址: http://blog.weiyigeek.top/2019/5/2-docker%E5%AD%A6%E4%B9%A0%E4%B9%8B%E5%9F%BA%E7%A1%80%E4%BD%B ...

  10. VScode链接服务器并配置公钥-SSH Keys

    VScode链接服务器并配置公钥-SSH Keys 一直在用Xshell做SSH连接服务器与虚拟机,但是中文乱码的问题一直找不到解决方案,干脆使用编辑器自带的插件,集成之后用起来也方便 1.概述 做法 ...