当前位置:网站首页>643. Maximum average number of subarrays I
643. Maximum average number of subarrays I
2022-07-01 14:20:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/maximum-average-subarray-i/
subject :
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 -10^4 <= nums[i] <= 10^4 |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/maximum-average-subarray-i
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
continuity k Traversal of numbers , hold k The number is regarded as one , It's like a window , Move back one by one
Record the value of the current window every time you move

First record the first group k value , Then move
new k The value is equal to The old value - The old first element + New elements
Method 1 、 The sliding window
#define mymax(a,b) ( (a) > (b) ? (a) : (b) )
double findMaxAverage(int* nums, int numsSize, int k){
int i;
int sum = 0;
double max = 0;
for(i=0; i<k; i++)
sum += nums[i];
max = sum;
for(i=k; i<numsSize; i++)
{
sum = sum - nums[i-k] + nums[i];
max = mymax(max, sum);
}
return max/k;
}边栏推荐
- [flask] flask starts and implements a minimal application based on flask
- C语言课程设计题目
- Details of appium key knowledge
- After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse
- Admire, Ali female program undercover more than 500 black production groups
- Uni app realizes advertisement scroll bar
- 2022-2-15 learning the imitation Niuke project - Section 3 post details
- SWT/ANR问题--当发送ANR/SWT时候如何打开binder trace(BinderTraces)
- Sorting learning sorting
- Basic operation of queue (implemented in C language)
猜你喜欢

Admire, Ali female program undercover more than 500 black production groups

Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?

sqlilabs less10

算网融合赋能行业转型,移动云点亮数智未来新路标

leetcode622. Design cycle queue (C language)

App automation testing Kaiyuan platform appium runner

佩服,阿里女程序卧底 500 多个黑产群……

Oracle-数据库对象的使用

After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse

TDengine 连接器上线 Google Data Studio 应用商店
随机推荐
奔涌而来的数字化浪潮,将怎样颠覆未来?
SWT / anr problem - how to open binder trace (bindertraces) when sending anr / SWT
How will the surging tide of digitalization overturn the future?
Introduction to distributed transactions (Seata)
leetcode622.设计循环队列(C语言)
The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
leetcode622. Design cycle queue (C language)
【R语言数据科学】:机器学习常见评估指标
Today, with the popularity of micro services, how does service mesh exist?
GET请求如何传递数组参数
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
Applet - multiple text line breaks in view
建立自己的网站(21)
基于算力驱动、数据与功能协同的分布式动态(协同)渲染/功能运行时
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
QT learning management system
Go整合Logrus实现日志打印
Research Report on the development trend and competitive strategy of the global aviation leasing service industry
算网融合赋能行业转型,移动云点亮数智未来新路标