当前位置:网站首页>力扣 643. 子数组最大平均数 I
力扣 643. 子数组最大平均数 I
2022-07-07 17:53:00 【Tomorrowave】
643. 子数组最大平均数 I
给你一个由 n 个元素组成的整数数组 nums 和一个整数 k 。
请你找出平均数最大且 长度为 k 的连续子数组,并输出该最大平均数。
任何误差小于 10-5 的答案都将被视为正确答案。
示例 1:
输入:nums = [1,12,-5,-6,50,3], k = 4
输出:12.75
解释:最大平均数 (12-5-6+50)/4 = 51/4 = 12.75
示例 2:
输入:nums = [5], k = 1
输出:5.00000
提示:
n == nums.length
1 <= k <= n <= 105
-104 <= nums[i] <= 104
思路:
滑动窗口:先定义一个窗口从左向右移动,当长度不满足k,窗口一直→,当吗,满足k时,窗口判断最大值是否为当前最大值,若满足,继续执行向右移动,若长度超出了k那么窗口进行缩短
代码
class Solution:
def findMaxAverage(self, nums: List[int], k: int) -> float:
i,j= 0,-1
sumls=0
maxval=-10000000
while j<len(nums)-1 :
j+=1
sumls+=nums[j]
while j - i + 1 > k:
sumls -= nums[i]
i+=1
if j - i + 1 == k :
maxval=max(maxval,sumls)
return maxval/k
边栏推荐
- Unable to link the remote redis server (solution 100%
- torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
- Sword finger offer II 013 Sum of two-dimensional submatrix
- LeetCode_7_5
- R language dplyr package select function, group_ The by function, filter function and do function obtain the third largest value of a specific numerical data column in a specified level in a specified
- 9 原子操作类之18罗汉增强
- Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
- 强化学习-学习笔记8 | Q-learning
- 最多可以参加的会议数目[贪心 + 优先队列]
- R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize the violin diagram, set the palette parameter to customize the filling color of violin diagrams at different
猜你喜欢

ASP.NET幼儿园连锁管理系统源码

The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list

mock. JS returns an array from the optional data in the object array

Kirin Xin'an cloud platform is newly upgraded!

Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)

Kirin Xin'an joins Ningxia commercial cipher Association

Flink并行度和Slot详解

Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses

一张图深入的理解FP/FN/Precision/Recall

【RT-Thread env 工具安装】
随机推荐
怎么在手机上买股票开户 股票开户安全吗
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record
The DBSCAN function of FPC package of R language performs density clustering analysis on data, checks the clustering labels of all samples, and the table function calculates the two-dimensional contin
【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
线性基
LeetCode 648(C#)
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
Welcome to the markdown editor
【STL】vector
小试牛刀之NunJucks模板引擎
位运算介绍
ASP. Net gymnasium integrated member management system source code, free sharing
Unable to link the remote redis server (solution 100%
注解。。。
Flink并行度和Slot详解
2022年投资哪个理财产品收益高?
# 欢迎使用Markdown编辑器
Sword finger offer II 013 Sum of two-dimensional submatrix
9 原子操作类之18罗汉增强