当前位置:网站首页>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
边栏推荐
- Interpretation of transpose convolution theory (input-output size analysis)
- pom.xml 配置文件标签作用简述
- Throughput
- Cuda版本不一致,编译apex报错
- 最多可以参加的会议数目[贪心 + 优先队列]
- Chapter 20 using work queue manager (3)
- 整型int的拼接和拆分
- vulnhub之tre1
- 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
- Introduction to bit operation
猜你喜欢

关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】

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

Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )

九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!

力扣 599. 两个列表的最小索引总和

干货分享|DevExpress v22.1原版帮助文档下载集合

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

mock.js从对象数组中任选数据返回一个数组

Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!

Simulate the implementation of string class
随机推荐
[RT thread env tool installation]
Notes...
强化学习-学习笔记8 | Q-learning
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
mysql 的一些重要知识
Data island is the first danger encountered by enterprises in their digital transformation
Force buckle 1790 Can two strings be equal by performing string exchange only once
Force buckle 459 Duplicate substring
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix
Force buckle 88 Merge two ordered arrays
pom.xml 配置文件标签作用简述
Semantic slam source code analysis
ASP.NET学习& asp‘s one word
【STL】vector
LeetCode_ 7_ five
[confluence] JVM memory adjustment
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip plot, set the position parameter, and configure the separation degree of different grouped
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
R language ggplot2 visualization: use the ggecdf function of ggpubr package to visualize the grouping experience cumulative density distribution function curve, and the linetype parameter to specify t