当前位置:网站首页>Force buckle 643 Subarray maximum average I
Force buckle 643 Subarray maximum average I
2022-07-07 20:06:00 【Tomorrowave】
643. Maximum average of subarrays I
Here you are n An integer array of elements nums And an integer k .
Please find the largest average and The length is k A continuous subarray of , And output the maximum average .
Any error less than 10-5 All answers will be considered correct .
Example 1:
Input :nums = [1,12,-5,-6,50,3], k = 4
Output :12.75
explain : Maximum average (12-5-6+50)/4 = 51/4 = 12.75
Example 2:
Input :nums = [5], k = 1
Output :5.00000
Tips :
n == nums.length
1 <= k <= n <= 105
-104 <= nums[i] <= 104
Ideas :
The sliding window : First define a window to move from left to right , When the length does not meet k, The window keeps →, When , Satisfy k when , The window determines whether the maximum value is the current maximum value , If meet , Continue to move right , If the length exceeds k Then shorten the window
Code
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
边栏推荐
- 力扣 459. 重复的子字符串
- Introduction to bit operation
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
- 干货分享|DevExpress v22.1原版帮助文档下载集合
- Force buckle 1232 Dotted line
- Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
- Interpretation of transpose convolution theory (input-output size analysis)
- 8 CAS
- 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
- Openeuler prize catching activities, to participate in?
猜你喜欢
【STL】vector
mock.js从对象数组中任选数据返回一个数组
华南X99平台打鸡血教程
MRS离线数据分析:通过Flink作业处理OBS数据
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
Welcome to the markdown editor
Openeuler prize catching activities, to participate in?
项目经理『面试八问』,看了等于会了
最多可以参加的会议数目[贪心 + 优先队列]
随机推荐
整型int的拼接和拆分
力扣 1037.有效的回旋镖
Make this crmeb single merchant wechat mall system popular, so easy to use!
Equals method
Introduction to bit operation
mysql 的一些重要知识
LeetCode_7_5
力扣 2319. 判断矩阵是否是一个 X 矩阵
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
编译器优化那些事儿(4):归纳变量
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
使用高斯Redis实现二级索引
ASP.NET学习& asp‘s one word
gorilla官方:golang开websocket client的示例代码
841. String hash
【Auto.js】自动化脚本
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
R language uses ggplot2 function to visualize the histogram distribution of counting target variables that need to build Poisson regression model, and analyzes the feasibility of building Poisson regr
Simulate the implementation of string class
剑指 Offer II 013. 二维子矩阵的和