当前位置:网站首页>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
边栏推荐
- Ways to improve the utilization of openeuler resources 01: Introduction
- 力扣 459. 重复的子字符串
- 841. String hash
- 831. KMP字符串
- The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
- BI的边界:BI不适合做什么?主数据、MarTech?该如何扩展?
- Cloud component development and upgrading
- [auto.js] automatic script
- Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
- 剑指 Offer II 013. 二维子矩阵的和
猜你喜欢

Force buckle 2319 Judge whether the matrix is an X matrix

Ways to improve the utilization of openeuler resources 01: Introduction

Data island is the first danger encountered by enterprises in their digital transformation

整型int的拼接和拆分

The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?

PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!

开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!

ASP. Net kindergarten chain management system source code

Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!

mysql 的一些重要知识
随机推荐
力扣 912.排序数组
关于自身的一些安排
ASP. Net gymnasium integrated member management system source code, free sharing
Force buckle 599 Minimum index sum of two lists
力扣 1232.缀点成线
华南X99平台打鸡血教程
sql 常用优化
equals 方法
pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Force buckle 599 Minimum index sum of two lists
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
JVM GC垃圾回收简述
力扣674. 最长连续递增序列
TS快速入门-泛型
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
YoloV6:YoloV6+Win10---训练自己得数据集
mock.js从对象数组中任选数据返回一个数组
openEuler 有奖捉虫活动,来参与一下?
【STL】vector