Maximum Average Subarray I

    这道题为简单题

  题目:

    

  思路:

    我先把前k个数加起来计算平均值,然后遍历列表从索引(k, len(nums)),每次列表向右移动一次就把最左边的的元素减去再加上最新的这个元素,计算总值再求平均值,并与之前的最大值比较,遍历结束后可得最大值。

  代码:

 1 class Solution(object):
 2     def findMaxAverage(self, nums, k):
 3         """
 4         :type nums: List[int]
 5         :type k: int
 6         :rtype: float
 7         """
 8         total = 0.0
 9         for i in range(k):
10             total += nums[i]
11         m = total / k
12
13         for i in range(k, len(nums)):
14             total = (total-nums[i-k]+nums[i])
15             m = max(m, total / k)
16         return m
时间: 2024-08-09 14:41:43

Maximum Average Subarray I的相关文章

643. Maximum Average Subarray I 最大子数组的平均值

[抄题]: 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:

LeetCode Maximum Average Subarray I

原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/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 v

643. Maximum Average Subarray I 最大子阵列平均数

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 

643. Maximum Average Subarray I

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: Maximu

[LeetCode] Maximum Average Subarray I 子数组的最大平均值

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: Maximu

LeetCode算法题-Maximum Average Subarray I(Java实现)

这是悦乐书的第278次更新,第294篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第146题(顺位题号是643).给定由n个整数组成的数组,找到具有最大平均值的长度为k的连续子数组,并输出最大平均值.例如: 输入:[1,12,-5,-6,50,3],k = 4 输出:12.75 说明:最大平均值为(12-5-6 + 50)/ 4 = 51/4 = 12.75 注意: 1 <= k <= n <= 30,000. 给定数组的元素将在[-10,000,10,00

[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:

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product =