当前位置:网站首页>力扣 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
边栏推荐
- MySQL、sqlserver oracle数据库连接方式
- 转置卷积理论解释(输入输出大小分析)
- Mysql, sqlserver Oracle database connection mode
- The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
- R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
- Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
- 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
- 吞吐量Throughout
- Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
- 小试牛刀之NunJucks模板引擎
猜你喜欢

openEuler 资源利用率提升之道 01:概论

Kirin Xin'an cloud platform is newly upgraded!

谷歌seo外链Backlinks研究工具推荐

国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报

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

多个线程之间如何协同

Openeuler prize catching activities, to participate in?

Simulate the implementation of string class

爬虫实战(七):爬王者英雄图片

The project manager's "eight interview questions" is equal to a meeting
随机推荐
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
How to buy bank financial products? Do you need a bank card?
Interpretation of transpose convolution theory (input-output size analysis)
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
LeetCode 648(C#)
what‘s the meaning of inference
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
模拟实现string类
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
Mysql, sqlserver Oracle database connection mode
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
pom.xml 配置文件标签作用简述
ant desgin 多选
Detailed explanation of Flink parallelism and slot
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
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
Time tools