原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/

题目:

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

Note:

  1. 1 <= k <= n <= 30,000.
  2. Elements of the given array will be in the range [-10,000, 10,000].

题解:

维护长度为k的sliding window的最大sum.

Time Complexity: O(nums.length). Space: O(1).

AC Java:

 class Solution {
public double findMaxAverage(int[] nums, int k) {
if(nums == null || nums.length < k){
throw new IllegalArgumentException("Illegal input array length smaller than k.");
} long sum = 0;
for(int i = 0; i<k; i++){
sum += nums[i];
} long max = sum;
for(int i = k; i<nums.length; i++){
sum += nums[i]-nums[i-k];
max = Math.max(max, sum);
}
return max*1.0/k;
}
}

LeetCode Maximum Average Subarray I的更多相关文章

  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] Maximum Average Subarray I 子数组的最大平均值

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

  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 643. 子数组最大平均数 I(Maximum Average Subarray I)

    643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...

  5. Maximum Average Subarray II LT644

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

  6. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  7. Maximum Average Subarray

    Given an array with positive and negative numbers, find the maximum average subarray which length sh ...

  8. leetcode644. Maximum Average Subarray II

    leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...

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

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

随机推荐

  1. C51数据类型

  2. VC6.0中添加库文件和头文件

    附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在“ ...

  3. 【CodeChef】Enormous Input Test

    The purpose of this problem is to verify whether the method you are using to read input data is suff ...

  4. linux基础(2)-网卡配置

    常用网卡配置参数 DEVICE=eth0     #指出设备名称 HWADDR=00:0C:29:3C:D2:CA     #网卡的mac地址TYPE=Ethernet     #网络类型为Ether ...

  5. SpringCloud-路由网关(Zuul)

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.只能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统. 在Spring Cloud ...

  6. Ubuntu 安装mysql

    ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server   2. apt-get isntall mysql-clie ...

  7. python基础1 - 多文件项目和代码规范

    1. 多文件项目演练 开发 项目 就是开发一个 专门解决一个复杂业务功能的软件 通常每 一个项目 就具有一个 独立专属的目录,用于保存 所有和项目相关的文件 –  一个项目通常会包含 很多源文件 在 ...

  8. VS2010 + QT 5 +open inventor 环境配置

    本科毕业设计做的是   随钻测量的井眼轨迹和测井曲线的三维显示  要求的环境是  QT +  Open Inventor    在寒假开学前,打算将环境配置好,开学后再正式编码实现,可是....环境也 ...

  9. Javascript-理解事件总结

    事件 [事件流]表述的是从页面接收事件的顺序.1.事件冒泡流:事件开始时由最具体的元素接收,然后逐级向上传播到较为不具体的节点(文档).所有浏览器都支持.2.事件捕获:与事件冒泡相反,事件捕获的用意在 ...

  10. hzau 1210 Happiness

    1210: Happiness Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 1149  Solved: 301[Submit][Status][We ...