[leetcode-643-Maximum Average Subarray I]

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ? s. If there isn‘t one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

思路:

类似于滑动窗口,维护一个长为k的窗口。

double findMaxAverage(vector<int>& nums, int k)
    {
        int s = accumulate(nums.begin(), nums.begin() + k, 0), m = s;
        for (int i = k; i < nums.size(); i++) {
            s += nums[i] - nums[i - k];
            m = max(m, s);
        }
        return double(m) / k;
    }
时间: 2024-08-20 05:54:43

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

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

原题链接在这里: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

[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] 152. Maximum Product Subarray 求最大子数组乘积

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1]

【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 = 

LeetCode – Refresh – Maximum Product Subarray

Similar to maximum sum subarray, but need a gmin to record the global minimum to handle negative number multiplication. 1 class Solution { 2 public: 3 int maxProduct(int A[], int n) { 4 int result = A[0], gMax = A[0], gMin = A[0]; 5 for (int i = 1; i

LeetCode:Maximum Product Subarray

题目: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 = 6. 这道题属于动态规划的题型,

[Leetcode] 1120. Maximum Average Subtree

Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subtree of a tree is any node of that tree plus all its descendants. The average value of a tree is the sum of its values, divided by the number of nodes.