①英文题目

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 <= k <= n <= 30,000.
Elements of the given array will be in the range [-10,000, 10,000].

②中文题目

给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。

示例 1:

输入: [1,12,-5,-6,50,3], k = 4
输出: 12.75
解释: 最大平均数 (12-5-6+50)/4 = 51/4 = 12.75

注意:

1 <= k <= n <= 30,000。
所给数据范围 [-10,000,10,000]。

③思路

就是个遍历,去k长度的窗口加和求平均,再滑动这个k长度窗口一步。我自己的思路写出来的代码,太慢了,约1300ms的速度。

④我自己的代码(1300ms级速度)

 class Solution {
public double findMaxAverage(int[] nums, int k) {
double sum=0;
double result=0;
double[] aver=new double[nums.length-k+1];
for(int i=0;i<=nums.length-k;i++){
for(int j=i;j<i+k;j++){
sum+=nums[j];
}
aver[i]=sum/k;
sum=0;
}
Arrays.sort(aver);
result =aver[nums.length-k];
return result;
}
}

⑤看了别人的代码之后,照着ta的思路改了之后,速度是4ms

 class Solution {
public double findMaxAverage(int[] nums, int k) {
double sum=0;
double result=0;
double[] aver=new double[nums.length-k+1];
double max=-10000;
for(int i=0;i<k;i++){
sum=sum+nums[i];
result=sum/k;
}
for(int i=1;i<=nums.length-k;i++){
sum=sum-nums[i-1]+nums[i+k-1];
if((sum/k)>result)
result=sum/k;
} return result;
}
}

ta比我更先进的思路在于,减少了加法的次数。比如,我每次都要把k窗口里的所有元素加一遍,而它是在上一次k窗口的加和的基础上,减去首部,再在尾部的下一个元素加进来。

比如

比如 [1,2,3,4,5,6] k=4

temp1 = 1+2+3+4

temp2 = temp1+5-1

⑥笔记:1、java里,Math.max只是用于求两个数中的较大值,而不是求一个数组里的最大值。同理min。

2、Array.sort()函数内部使用的是快排,结果是在原数组的基础上,变成一个升序,比如

Array.sort(arr);

让arr变成升序了。

[LC]643题 Maximum Average Subarray I(子数组最大平均数 I)的更多相关文章

  1. leetcode 643. Maximum Average Subarray I 子数组最大平均数 I

    一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...

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

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

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

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

  6. LeetCode算法题-Maximum Average Subarray I(Java实现)

    这是悦乐书的第278次更新,第294篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第146题(顺位题号是643).给定由n个整数组成的数组,找到具有最大平均值的长度为k的 ...

  7. LeetCode OJ:Maximum Product Subarray(子数组最大乘积)

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

  8. LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)

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

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

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

随机推荐

  1. python学习(内置函数)

    1.id()返回对象的内存地址 a = 1 print id(a) print id(1) 2.int()用于将数据类型转换为整型 a = " b = 2 print int(a) + b ...

  2. 过waf实战之文件上传bypass总结

    这里总结下关于waf中那些绕过文件上传的姿势和尝试思路 环境 apache + mysql +php waf:某狗waf 这里写了一个上传页面 <html> <body> &l ...

  3. PHP current

    1.函数的作用:返回数组的当前元素 2.函数的参数: @params array &$array 3.例子: <?php $arr = [null,'PK',false]; : ; ec ...

  4. Centos 7 集成安装Apache+PHP+Kerberos+LDAP+phpLDAPadmin

    一.安装Apache 1.1.安装Apache Apache程序是目前拥有很高市场占有率的Web服务程序之一,其跨平台和安全性广泛被认可且拥有快速.可靠.简单的API扩展. 它的名字取自美国印第安人土 ...

  5. BeetleX服务网关之服务发现与泛域名路由

    在新版本的服务网关中提供了服务发现和泛域名路由解决功能,服务发现可以在无须配置的情况下实现服务自动注册到网关中解脱对服务配置的繁琐工作:而泛域名路由则可以针对不同的域名制定不同的负载规则. 使用con ...

  6. Newtonsoft—Json.NET常用方法简述

    Json.NET常用方法汇总(可解决日常百分之90的需求) 0.Json.NET基础用法 首先去官网下载最新的Newtonsoft.Json.dll(也可以使用VS自带的NuGet搜索Json.NET ...

  7. 通过FeignClient接收shaded的javabean的JSON序列化

    问题说明 最近做了关于flink的需求. 现在需要通过HTTP访问FLINK的 RESTAPI, rest 接口的JSON 非常庞大而复杂. 那么怎么去完整的接收数据呢? 方法一就是手写部分需要的Ja ...

  8. Spring之Zuul初步使用(十)

    一.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. ...

  9. Windows下NFS服务器SFU设置(可以共享linux系统)

    一.安装SFU1.下载软件SFU http://download.microsoft.com/download/a/1/c/a1ca7af1-a6e3-46e7-874a-4c5d8c0fb3b7/S ...

  10. django & celery - 关于并发处理能力和内存使用的小结

    背景 众所周知,celery 是python世界里处理分布式任务的好助手,它的出现结合赋予了我们强大的处理异步请求,分布式任务,周期任务等复杂场景的能力. 然鹅,今天我们所要讨论的则是如何更好的在使用 ...