LeetCode.643 Maximum Average Subarray I
来源:互联网 发布:tiu网络用语什么意思 编辑:程序博客网 时间:2024/06/05 10:59
题目:
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 = 4Output: 12.75Explanation: 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].
class Solution { public double findMaxAverage(int[] nums, int k) { //给定数组,找出连续的子串(其中平均值最大),返回该最大的子串平均值 //思路:使用PreSum求解 int [] preSum=new int[nums.length]; preSum[0]=nums[0]; //求和 for(int i=1;i<nums.length;i++){ preSum[i]=nums[i]+preSum[i-1]; } //求解 //初始k个元素和 int max=preSum[k-1]; for(int i=k;i<nums.length;i++){ int temp=preSum[i]-preSum[i-k]; max=Math.max(max,temp); } return max/1.0/k; }}
阅读全文
0 0
- Leetcode 643 Maximum Average Subarray I
- LeetCode 643 Maximum Average Subarray I
- Leetcode 643 Maximum Average Subarray I
- leetcode 643 Maximum Average Subarray I
- LeetCode.643 Maximum Average Subarray I
- leetcode 643[easy]--Maximum Average Subarray I
- leetcode[Maximum Average Subarray I]
- leetCode-Maximum Average Subarray I
- 643 Maximum Average Subarray I
- LeetCode 643. Maximum Average Subarray I
- [LeetCode]643. Maximum Average Subarray I
- [LeetCode] 643. Maximum Average Subarray I
- LeetCode刷题【Array】 Maximum Average Subarray I
- LeetCode 643. Maximum Average Subarray I
- leetcode 643. Maximum Average Subarray I
- [LeetCode]643. Maximum Average Subarray I
- leetcode 643. Maximum Average Subarray I
- LeetCode 643. Maximum Average Subarray I
- Linux系列:Linux中如何安装.rpm、.tar、.tar.gz和tar.bz2文件
- Kotlin学习教程(一)
- POI读取Excel 各种特殊数字和类型的转换。
- Python 中的 if __name__ == '__main__' 该如何理解
- LeetCode 2.add Two Numbers
- LeetCode.643 Maximum Average Subarray I
- 你所不知道的21个云服务器的应用实践———云计算综合入门指南
- [Leetcode] 479. Largest Palindrome Product 解题报告
- linux 与openwrt路由器传输文件
- Babel——ES6转换
- Android Studio3.0开发JNI流程------在JNI中输出打印日志
- 单例模式
- 解决MySQL数据库不区分大小写的问题
- 调模数转换器采集板的感悟(不间断更新)