本文主要是介绍LeetCode 643.Maximum Average Subarray 最大子数组的平均值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
LeetCode 643.Maximum Average Subarray 最大子数组平均值
Description:
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
Example 2:
Input: [4,0,4,,3,3], k = 5
Output: 2.8
Explanation: Maximum average is (4+0+4+3+3)/5 = 14/5 = 2.8
分析:
之前已经做过求最大子数组和的问题,所以我们可以按照类似的思路来解决这道题。
首先求出数组的累计和,即
这篇关于LeetCode 643.Maximum Average Subarray 最大子数组的平均值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!