[抄题]:

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. C++将链表反转的实现

    有题目的需求是求将链表反转,例如1->2->3->4->5转变成5->4->3->2->1,经典的是可以有两种解决方法,递归方式和非递归方式,下面给出C ...

  2. 深入学习Heritrix---解析Frontier(链接工厂)(转)

    深入学习Heritrix---解析Frontier(链接工厂) Frontier是Heritrix最核心的组成部分之一,也是最复杂的组成部分.它主要功能是为处理链接的线程提供URL,并负责链接处理完成 ...

  3. C++之指针使用

    C++指针使用的好坏直接反映了编程人员水平的高低,下面从指针和数组的区别.指针参数是如何传递内存.野指针.malloc/free.new/delete和内存耗尽怎么办方面进行总结. 一 指针和数组对比 ...

  4. storm-kafka源码走读之KafkaSpout

    from: http://blog.csdn.net/wzhg0508/article/details/40903919 (五)storm-kafka源码走读之KafkaSpout 原创 2014年1 ...

  5. spring mvc加了@produces注解后报406

    问题背景:调用http的post接口返回一个String类型的字符串时中文出现乱码,定位出问题后在@RequestMapping里加produces注解produces = "applica ...

  6. Android之Application类用法

    Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系统的一些信息. Andro ...

  7. laravel中有条件使用where

    在项目开发的过程中;有时候会有多个参数 去用在where查询中;那么这里的where语句是可能有也可能没有的 1.用原生的mysql语句来实现 private function getData($ty ...

  8. java代码从键盘输入n的值,计算1+1/2+1/3+...+1/n的值,,

    总结:谢谢陈勇老师.很棒的指导.超有爱. 总是不思考++++如内存的分析.堆和栈.堆内存里对象,字符串,栈里基本数据类型 来龙去脉,属性方法的调用,都不是很理解.... package com.c2; ...

  9. juc线程池原理(五):拒绝策略示例

    概要 拒绝策略介绍 线程池的拒绝策略,是指当任务添加到线程池中被拒绝,而采取的处理措施.当任务添加到线程池中之所以被拒绝,可能是由于:第一,线程池异常关闭.第二,任务数量超过线程池的最大限制. 线程池 ...

  10. Java-Runoob-高级教程:Java 9 新特性

    ylbtech-Java-Runoob-高级教程:Java 9 新特性 1.返回顶部 1. Java 9 新特性 Java 9 发布于 2017 年 9 月 22 日,带来了很多新特性,其中最主要的变 ...