[抄题]:

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.

Example 1:

Input: [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

sliding window 最一般的步骤就是右加左减,直接写就行了

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

sliding window : 框的长度恒定

[关键模板化代码]:

for (int i = k; i < nums.length; i++) {
sum = sum + nums[i] - nums[i - k];

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

644. Maximum Average Subarray II 长度可以更长。贼复杂的二分法,有点无聊。

[代码风格] :

class Solution {
public double findMaxAverage(int[] nums, int k) {
//ini, calculate the first k
int sum = 0;
for (int i = 0; i < k; i++) {
sum += nums[i];
}
int max = sum; //for loop, sliding window
for (int i = k; i < nums.length; i++) {
sum = sum + nums[i] - nums[i - k];
max = Math.max(max, sum);
} //return
return max / 1.0 / k;
}
}

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

  1. 【Leetcode_easy】643. Maximum Average Subarray I

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

  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] 644. Maximum Average Subarray II 子数组的最大平均值之二

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

  4. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  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. 643. Maximum Average Subarray

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

  7. [Leetcode]643. Maximum Average Subarray I

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

  8. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  9. [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 ...

随机推荐

  1. 【Javascrpt 速成篇】 一:js基础

     本系列文章Javascript一律简称js,javascript太长了((⊙﹏⊙)b) js概述 js是面向对象和基于事件驱动的解释型语言,主要用于WEB前端,处理用户交互.几年前js只是作为一种前 ...

  2. bzoj 4448 情报传递

    Written with StackEdit. Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有\(n\)名情报员.每名情报员能有若干名(可能没有)下线,除\ ...

  3. python3 tesserocr 安装 来解决部分爬虫遇到的字符识别问题

    1. OCR OCR,即Optical Character Recognition,光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程.对于图形验证码来说,它们都是一些不规则的字符 ...

  4. hadoop文件IO

    InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符.它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集. Input ...

  5. bzoj 4650 & 洛谷 P1117 优秀的拆分 —— 枚举关键点+后缀数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4650 https://www.luogu.org/problemnew/show/P1117 ...

  6. Java 传递可变参数和方法重载

    形式:类型... 参数名 示例:public void show(int... a) {}; 可变参数在方法中被当作数组来处理 可变参数传值的四种方式: 一个值也不传,可变参数会接收到长度为0的数组 ...

  7. python基础篇之进阶

    python基础篇之进阶 参考博客:http://www.cnblogs.com/wupeiqi/articles/5115190.html python种类 1. cpython  使用c解释器生产 ...

  8. Does Windows have a limit of 2000 threads per process?

    http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx Often I see people asking why the ...

  9. ONVIF、RTSP/RTP、FFMPEG的开发实录

    前言 本文从零基础一步步实现ONVIF协议.RTSP/RTP协议获取IPC实时视频流.FFMPEG解码.开发环境为WIN7 32位 + VS2010. 最终成功获取浩云.海康.大华的IPC实时视频流. ...

  10. juc线程池原理(二):ThreadPoolExecutor的成员变量介绍

    概要 线程池的实现类是ThreadPoolExecutor类.本章,我们通过分析ThreadPoolExecutor类,来了解线程池的原理. ThreadPoolExecutor数据结构 Thread ...