[leetcode-644-Maximum Average Subarray II]
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 <=
k<=n<= 10,000. - Elements of the given array will be in range [-10,000, 10,000].
- 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]的更多相关文章
- [LeetCode] 644. Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- 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 ...
- leetcode644. Maximum Average Subarray II
leetcode644. Maximum Average Subarray II 题意: 给定由n个整数组成的数组,找到长度大于或等于k的连续子阵列,其具有最大平均值.您需要输出最大平均值. 思路: ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- Maximum Average Subarray II LT644
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- Maximum Average Subarray II
Description Given an array with positive and negative numbers, find the maximum average subarray whi ...
- LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [Leetcode]643. Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [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 ...
- leetcode 643. Maximum Average Subarray I 子数组最大平均数 I
一.题目大意 https://leetcode.cn/problems/maximum-average-subarray-i/ 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k . 请你 ...
随机推荐
- Java虚拟机垃圾回收:基础点(转载)
1.Java虚拟机垃圾回收 垃圾回收,或称垃圾收集(Garbage Collection,GC)是指自动管理回收不再被引用的内存数据. 在1960年诞生于MIT的Lisp语言首次使用了动态内存分配和垃 ...
- Oracle 差异性增量 和 累计增量 原理(转)
RMAN一个强大的功能是支持增量备份,增量备份中心思想就是减少备份的数据量,我们不需要在从头开始备份了,只需要备份自上次已备份之后的数据块即可. Oracle 9i 共有五种级别 0 1 2 3 ...
- 姆洋自主研发堆(heap)头文件
这是姆洋自主研发的heap头文件 将其录入IDE,并保存为heap.h,保存在存放C++头文件的文件夹里(我只知道Dev-C++是Dev-cpp/MinGW64/lib/gcc/x86_64-w64- ...
- chromium之pickle
pickle谷歌翻译成泡菜 醉了,看一下头文件的说明 // This class provides facilities for basic binary value packing and unpa ...
- EF core Code First 简单的使用方法
好吧,我又回来了,其实一直都想写一篇关于EF core 的文章去记录自己在开发时候遇到的问题. 为什么要使用EF框架呢,因为原始的ADO.NET需要编写大量的数据访问代码,所以使用EF会更方便.但是今 ...
- ci框架 用框架自带db 添加括号,比如 like 等等左右添加括号 解决办法
$this->load->model('station/Station_model','Station'); // East // North $this->Station-> ...
- vsftpd文件服务参数汇总和虚拟用户使用
FTP文件传输协议 FTP协议特点 基于C/S结构 双通道协议:数据和命令连接 数据传输格式:二进制(默认)和文本(w文本格式会修改文件内容) 两种模式:服务器角度 主动(PORT style):服务 ...
- LeetCode-环形链表II
LeetCode-环形链表II 为找到入口点可以用以下方法 使用快慢指针法直到两个指针相遇 头节点处创建一个新的指针,并且向前移动,两个指针相遇处创建一个新的指针,并且向前移动,直到两个指针相遇为入口 ...
- Redis面试问题
下面列出的这些其中有一些是我面试时遇到的,但是当时自己还不会,所以在网站上找了以下,然后整理出来,加强记忆 感谢码洞将这些问题整理出来: Redis有哪些数据结构? 字符串String.字典Hash. ...
- BigData--hadoop集群搭建之zookeer安装
Zookeeper安装 cd /opt/ tar -zxvf zookeeper-3.4.10.tar.gzmv zookeeper-3.4.10 /opt/zookeeper修改配置文件cd /o ...