原题链接在这里: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. spring data jpa是什么?

    JPA是一个Java编程语言接口规范,Hibernate ORM是JPA规范的一个实现.   Spring Data JPA能干什么 在开始之前,先举个简单的例子. 一张表user有三个字段,id.n ...

  2. [转]springmvc中的常用的返回

    package com.boventech.learning.controller; import java.util.HashMap; import java.util.Map; import or ...

  3. 黑色CSS3立体动画菜单

    在线演示 本地下载

  4. Class文件结构(更新中)

    Class文件是一组以8位字节为单位的二进制流,当遇见需要占用8位字节以上空间的数据项时,则会按照高位在前的方式分割成若干个8位字节进行存储. 格式,采用伪结构,只有两种数据结构:无符号数和表. 无符 ...

  5. mini2440移植uboot 2014.04(五)

    代码上传到github上:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440 前几篇博文: <mini2440移植uboot 2014.04 ...

  6. Kubernetes 部署Weave Scope监控

    yaml下载地址: https://cloud.weave.works/k8s/scope.yaml?k8s-version=? 目前有以下几个版本: ["v1.4"," ...

  7. ubuntu centos macos 配置上网代理

    因为我国强大的GFW,导致很多国外的应用无法安装,因为需要在系统中配置http/https代理. Ubuntu代理配置 配置方式非常简单,在~/.bashrc文件中增加: echo "exp ...

  8. Freemarker中大于号>的使用

    在Freemarker中,比较数据的大小时候,要注意大于号(>)的使用.如果不注意,程序就会发生异常信息,如下面的例子:   1 2 3 4 <#assign x = 4> < ...

  9. Excel下载打不开

    1.问题描述:今天遇到个问题,对于定时发送邮件,前两天还正常,今天发现邮件能收到,但打不开,显示如下错误: 预览邮件显示: 点击Excel打开,显示如下: 2.问题解决方案 删除对于服务器上部分空间内 ...

  10. 自学Hadoop

    一.Hadoop基础设施 起源于Google的三篇论文: 1. <The Google File System > 2003年 http://static.googleuserconten ...