Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to 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:
when length is 5, maximum average value is 10.8,
when length is 6, maximum average value is 9.16667.
Thus return 12.75.

Note:

  1. 1 <= k <= n <= 10,000.
  2. Elements of the given array will be in range [-10,000, 10,000].
  3. The answer with the calculation error less than 10-5 will be accepted

思路:

用一个sum去记录从开始到当前元素之前的所有元素的和,即sum[i]记录下标从0到i-1的所有元素的和。

这样区间[i,j]的和就可以表示为sum[j+1] - sum[i].

还发现 两句话效率不一样,上一句会超时。。。

 ret = max(ret,(sum[i+window]-sum[i])/window);//超时。。。
if (ret*window < (sum[i + window] - sum[i]))ret = (sum[i + window] - sum[i])/window;
double findMaxAverage1(vector<int>& nums, int k)
{
int n = nums.size();
vector<double>sum(n + ,);
for (int i = ; i < n; i++)sum[i + ] = sum[i] + nums[i];
double ret = -1e4;
for (int i = ; i <= n - k;i++)
{
for (int window = k; window + i <= n;window++)
{
//ret = max(ret,(sum[i+window]-sum[i])/window);//超时。。。
if (ret*window < (sum[i + window] - sum[i]))ret = (sum[i + window] - sum[i])/window;
}
}
return ret;
}

这种方法效率不是很高,看到有用二分查找思路的,还不是很懂,待优化。

[leetcode-644-Maximum Average Subarray II]的更多相关文章

  1. [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二

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

  2. LC 644. Maximum Average Subarray II 【lock,hard】

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

  3. leetcode644. Maximum Average Subarray II

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

  4. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

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

  5. Maximum Average Subarray II LT644

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

  6. Maximum Average Subarray II

    Description Given an array with positive and negative numbers, find the maximum average subarray whi ...

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

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

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

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

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

随机推荐

  1. PowerDesign16.6支持Mysql的生成sql脚本中包含Collate信息

    当前powerDesign版本:16.6 列上指定:Collation = utf8_general_ci 但是SQL脚本中,列字段没有显示Collect ---------------------- ...

  2. 指令过滤器orderBy

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章,讲了 ...

  4. 字段中有空的时候 进行逻辑运算,mysql 与 oracle 处理函数IFNULL() 与 nvl() ,选取NULL 值 。

    mySQL数据库: SELECT id_p,IFNULL(math,0)+IFNULL(english,0) 总分 from mytest_brian1 Oracle 数据库: select  id_ ...

  5. 通过swagger下载的文件乱码解决方法,求解

    这里的数据显示 点击Download Templates下载之后是显示一个response流都不是一个xlsx文件 这个是由什么原因造成的,求解?

  6. Hadoop(20)-MapReduce框架原理-OutputFormat

    1.outputFormat接口实现类 2.自定义outputFormat 步骤: 1). 定义一个类继承FileOutputFormat 2). 定义一个类继承RecordWrite,重写write ...

  7. requests模块基础

    requests模块 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bor ...

  8. 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍

    参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...

  9. python2.7练习小例子(十七)

        17):题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制.     程序分析: ...

  10. linux进程 生产者消费者

    #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> # ...