Leetcode刷题83-643. 子数组最大平均数 I(C++详细解法!!!)

博客介绍了如何解决LeetCode中的第643题,即寻找数组中长度为k的连续子数组,使得其平均值最大。博主分享了自己最初的暴力解法因超时失败,然后介绍了使用动态规划和滑动窗口的AC解法,总结了自己的学习收获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Come from : [https://leetcode-cn.com/problems/maximum-average-subarray-i/]

1.Question

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 <= k <= n <= 30,000.
Elements of the given array will be in the range [-10,000, 10,000].

2.Answer

easy 类型题目。
我的暴力解题思路
依次求出 K 个元素的 平均值,找出最大的即可,但是 超出时间限制。。。

超时代码如下:(比较low。。。)

class Solution {
   
public:
    double findMaxAverage(vector<int>& nums, int k) {
   
        int maxAvg = I
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值