当前位置:网站首页>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
边栏推荐
- Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
- Implement secondary index with Gaussian redis
- 整型int的拼接和拆分
- 【Confluence】JVM内存调整
- 841. String hash
- JVM class loading mechanism
- ASP. Net kindergarten chain management system source code
- Cloud component development and upgrading
- 使用高斯Redis实现二级索引
猜你喜欢

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

mysql 的一些重要知识

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

华南X99平台打鸡血教程

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

整型int的拼接和拆分

Introduction to bit operation

Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"

CSDN syntax description

论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
随机推荐
JVM 类加载机制
openEuler 资源利用率提升之道 01:概论
YoloV6:YoloV6+Win10---训练自己得数据集
【Confluence】JVM内存调整
torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
力扣 912.排序数组
PMP每日一练 | 考试不迷路-7.7
PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
MRS离线数据分析:通过Flink作业处理OBS数据
MSE API学习
Ways to improve the utilization of openeuler resources 01: Introduction
整型int的拼接和拆分
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
pom. XML configuration file label: differences between dependencies and dependencymanagement
Sword finger offer II 013 Sum of two-dimensional submatrix
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
模拟实现string类
力扣 1037.有效的回旋镖
ASP. Net gymnasium integrated member management system source code, free sharing
Openeuler prize catching activities, to participate in?