难度:easy

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

Note:

  1. 1 <= k <= n <= 30,000.
  2. Elements of the given array will be in the range [-10,000, 10,000].
思路:求给定list里面一段长度为K的片段,使得该片段的具有最大平均值。
           遍历全表,找到所有可能的长度为K的片段,求其最大平均值。
           关于ans的初始值设定,开始设为0,或者None 均为错误,于是设置为第一个K片段的平均值。

leetcode 643[easy]--Maximum Average Subarray I


相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2021-06-26
  • 2021-10-14
  • 2021-08-06
  • 2021-08-01
猜你喜欢
  • 2021-09-23
  • 2021-12-16
  • 2021-10-21
  • 2021-08-08
  • 2022-12-23
  • 2021-08-21
  • 2021-07-09
相关资源
相似解决方案