[LeetCode Daily Question] - 643. The maximum average number of sub-arrays I

One [topic category]

  • sliding window

Two [question difficulty]

  • Simple

Three [topic number]

  • 643. Maximum average number of subarrays I

Four [title description]

  • give you a by nnInteger arraynums nums of n elementsn u m s and an integerkkk
  • Please find the largest average and length kkContiguous subarrays of k , and output that maximum mean.
  • Any error less than 1 0 − 5 10^{-5}105 answers will be considered correct.

Five [topic example]

  • Example 1:

    • Input: nums = [1,12,-5,-6,50,3], k = 4
    • Output: 12.75
    • Explanation: Maximum average (12-5-6+50)/4 = 51/4 = 12.75
  • Example 2:

    • Input: nums = [5], k = 1
    • Output: 5.00000

Six [topic prompt]

  • n = = n u m s . l e n g t h n == nums.length n==nums.length
  • 1 < = k < = n < = 1 0 5 1 <= k <= n <= 10^{5} 1<=k<=n<=105
  • − 1 0 4 < = n u m s [ i ] < = 1 0 4 -10^{4} <= nums[i] <= 10^{4} 104<=nums[i]<=104

Seven [problem-solving ideas]

  • Using the idea of ​​sliding windows
  • First calculate the sum of the first k element values
  • Then enter the loop to traverse the remaining elements, subtract the first element in the "window" every time an element is traversed, and then add the newly traversed elements to the "window", then compare and take the maximum value
  • The title requires us to return the duoble type, so we need to perform a forced type conversion at the end, and divide the sum of the largest "window" by the number of window elements
  • Finally return the result

Eight 【Time Frequency】

  • Time complexity: O ( n ) O(n)O ( n ) , wherennn is the length of the incoming array
  • Space complexity: O ( 1 ) O(1)O(1)

Nine [code implementation]

  1. Java language version
class Solution {
    
    
    public double findMaxAverage(int[] nums, int k) {
    
    
        int tempMax = 0;
        for(int i = 0;i<k;i++){
    
    
            tempMax += nums[i];
        }
        int sum = tempMax;
        for(int i = k;i<nums.length;i++){
    
    
            sum = sum - nums[i - k] + nums[i];
            tempMax = Math.max(tempMax,sum);
        }
        return (double)tempMax / k;
    }
}
  1. C language version
double findMaxAverage(int* nums, int numsSize, int k)
{
    
    
    int tempSum = 0;
    for(int i = 0;i<k;i++)
    {
    
    
        tempSum += nums[i];
    }
    int sum = tempSum;
    for(int i = k;i<numsSize;i++)
    {
    
    
        sum = sum - nums[i - k] + nums[i];
        tempSum = fmax(tempSum,sum);
    }
    return (double)tempSum / k;
}
  1. Python language version
class Solution:
    def findMaxAverage(self, nums: List[int], k: int) -> float:
        tempSum = resSum = sum(nums[:k])
        for i in range(k,len(nums)):
            tempSum = tempSum - nums[i - k] + nums[i]
            resSum = max(resSum,tempSum)
        return resSum / k
  1. C++ language version
class Solution {
    
    
public:
    double findMaxAverage(vector<int>& nums, int k) {
    
    
        int tempSum = 0;
        for(int i = 0;i<k;i++)
        {
    
    
            tempSum += nums[i];
        }
        int sum = tempSum;
        for(int i = k;i<nums.size();i++)
        {
    
    
            sum = sum - nums[i - k] + nums[i];
            tempSum = max(tempSum,sum);
        }
        return (double)tempSum / k;
    }
};

Ten【Submission Results】

  1. Java language version
    insert image description here

  2. C language version
    insert image description here

  3. Python language version
    insert image description here

  4. C++ language version
    insert image description here

Guess you like

Origin blog.csdn.net/IronmanJay/article/details/129893456