当前位置:网站首页>力扣 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
边栏推荐
- 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
- how to prove compiler‘s correctness
- 【STL】vector
- 831. KMP字符串
- 九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
- 注解。。。
- Specify the version of OpenCV non-standard installation
- 浏览积分设置的目的
- Time tools
- Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记
猜你喜欢

转置卷积理论解释(输入输出大小分析)

The project manager's "eight interview questions" is equal to a meeting

Cloud 组件发展升级

Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
![[RT thread env tool installation]](/img/bc/9b39651d40a240f0893200793f67e9.png)
[RT thread env tool installation]

# 欢迎使用Markdown编辑器

【RT-Thread env 工具安装】

Install mysql8 for Linux X ultra detailed graphic tutorial

论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》

Flink并行度和Slot详解
随机推荐
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
How to buy bank financial products? Do you need a bank card?
R language ggplot2 visualization: use the ggdensity function of ggpubr package to visualize the packet density graph, and use stat_ overlay_ normal_ The density function superimposes the positive dist
编译器优化那些事儿(4):归纳变量
开源OA开发平台:合同管理使用手册
歌单11111
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
Kirin Xin'an cloud platform is newly upgraded!
IP tools
吞吐量Throughout
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
My creation anniversary
What does "true" mean
PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
tp6 实现佣金排行榜
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
Kirin Xin'an joins Ningxia commercial cipher Association
[confluence] JVM memory adjustment