[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笔试--代码纠错
package practice.javase; public abstract class Name { private String name; public abstract boolean i ...
- maven pom 增加本地jar 依赖
https://www.cnblogs.com/huhongy/p/7337280.html <dependency> <groupId>org.hamcrest</gr ...
- Oracle树形结构数据---常见处理情景
Oracle树形结构数据---常见处理情景 1.查看表数据结构 SELECT * FROM QIANCODE.TREE_HIS_TABLE T ORDER BY T.NODE_LEVEL; ...
- iOS | XIB简单应用和注意点
2018开篇第一篇文章,本文分享一点关于XIB的小知识,对于iOS开发新人来说或许有用. XIB 是 Interface Builder 的图形界面设计文档. 从Xcode 3.0 开始,苹果提供Xi ...
- iOS之一个iOS开发人员完整的学习路线
iOS开发能力 掌握(最好是精通)OC语言和runtime各种细节(读过相关的clang源码和runtime源码为佳).精通基本的framework(Foundation,UIKit等,平时干活用得最 ...
- vue.js中的slot
vue.js 中的 slot 一.slot 的作用 调用组件的时候,对于数据,我们会用props将数据从父组件传至子组件.但是,如果从父组件到子组件,单纯是页面局部渲染的改变,slot会更合适. 二. ...
- 剑指offer js算法练习(1-10)
1.二维数组中的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数, ...
- 【Nowcoder 上海五校赛】1 + 2 = 3?(斐波那契规律)
题目描述 小Y在研究数字的时候,发现了一个神奇的等式方程,他屈指算了一下有很多正整数x满足这个等式,比如1和2,现在问题来了,他想知道从小到大第N个满足这个等式的正整数,请你用程序帮他计算一下. (表 ...
- Mac上从gitlab上拉项目实战总结
建立公钥,私钥 https://blog.csdn.net/jigongdajiang/article/details/65441923 2019-01-03 比较喜欢使用图形化界面
- 服务器空间不足导致mysql服务器无法运行
今天有朋友请我帮忙解决一个问题,他公司服务器mysql数据库一直连接失败.登录服务期之后发现服务器空间占满了,导致mysql不能启动. 下面说解决方法: 首先查看空间占用,发现空间占满了 df -h ...