当前位置:网站首页>力扣 643. 子数组最大平均数 I
力扣 643. 子数组最大平均数 I
2022-07-07 17:53:00 【Tomorrowave】
643. 子数组最大平均数 I
给你一个由 n 个元素组成的整数数组 nums 和一个整数 k 。
请你找出平均数最大且 长度为 k 的连续子数组,并输出该最大平均数。
任何误差小于 10-5 的答案都将被视为正确答案。
示例 1:
输入:nums = [1,12,-5,-6,50,3], k = 4
输出:12.75
解释:最大平均数 (12-5-6+50)/4 = 51/4 = 12.75
示例 2:
输入:nums = [5], k = 1
输出:5.00000
提示:
n == nums.length
1 <= k <= n <= 105
-104 <= nums[i] <= 104
思路:
滑动窗口:先定义一个窗口从左向右移动,当长度不满足k,窗口一直→,当吗,满足k时,窗口判断最大值是否为当前最大值,若满足,继续执行向右移动,若长度超出了k那么窗口进行缩短
代码
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
边栏推荐
- The project manager's "eight interview questions" is equal to a meeting
- Training IX basic configuration of network services
- 一锅乱炖,npm、yarn cnpm常用命令合集
- Boot 和 Cloud 的版本选型
- 谷歌seo外链Backlinks研究工具推荐
- Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
- gorilla官方:golang开websocket client的示例代码
- 开源OA开发平台:合同管理使用手册
- 让这个 CRMEB 单商户微信商城系统火起来,太好用了!
- A pot of stew, a collection of common commands of NPM and yarn cnpm
猜你喜欢
位运算介绍
mock. JS returns an array from the optional data in the object array
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
Kirin Xin'an cloud platform is newly upgraded!
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
Nunjuks template engine
Cloud 组件发展升级
随机推荐
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
8 CAS
How to cooperate among multiple threads
How to buy stocks on your mobile phone and open an account? Is it safe to open an account
openEuler 有奖捉虫活动,来参与一下?
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
Sword finger offer II 013 Sum of two-dimensional submatrix
Boot 和 Cloud 的版本选型
注解。。。
Some arrangements about oneself
Specify the version of OpenCV non-standard installation
A pot of stew, a collection of common commands of NPM and yarn cnpm
线性基
Kirin Xin'an cloud platform is newly upgraded!
ASP. Net kindergarten chain management system source code
“本真”是什么意思
多个线程之间如何协同
The research group of the Hunan Organizing Committee of the 24th China Association for science and technology visited Kirin Xin'an
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
9 atomic operation class 18 Rohan enhancement