当前位置:网站首页>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
边栏推荐
- [confluence] JVM memory adjustment
- mysql 的一些重要知识
- Implement secondary index with Gaussian redis
- 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
- LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
- UCloud是基础云计算服务提供商
- The project manager's "eight interview questions" is equal to a meeting
- ASP. Net kindergarten chain management system source code
- 吞吐量Throughout
- Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
猜你喜欢

PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!

PMP practice once a day | don't get lost in the exam -7.7

LeetCode_ 7_ five
![最多可以参加的会议数目[贪心 + 优先队列]](/img/f3/e8e939e0393efc404cc159d7d33364.png)
最多可以参加的会议数目[贪心 + 优先队列]

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!!!

8 CAS

华南X99平台打鸡血教程

Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记

模拟实现string类

Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
随机推荐
【STL】vector
Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记
Implement secondary index with Gaussian redis
力扣 643. 子数组最大平均数 I
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
Force buckle 1790 Can two strings be equal by performing string exchange only once
JVM class loading mechanism
最多可以参加的会议数目[贪心 + 优先队列]
Force buckle 989 Integer addition in array form
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
力扣 88.合并两个有序数组
力扣 912.排序数组
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
使用高斯Redis实现二级索引
mock.js从对象数组中任选数据返回一个数组
sql 常用优化
PMP對工作有益嗎?怎麼選擇靠譜平臺讓備考更省心省力!!!
力扣 1790. 仅执行一次字符串交换能否使两个字符串相等
使用高斯Redis实现二级索引
vulnhub之school 1