当前位置:网站首页>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;
}边栏推荐
- QT学习管理系统
- 2022-2-15 learning the imitation Niuke project - post in Section 2
- 2022-2-15 learning the imitation Niuke project - Section 3 post details
- 基于算力驱动、数据与功能协同的分布式动态(协同)渲染/功能运行时
- Distributed dynamic (collaborative) rendering / function runtime based on computing power driven, data and function collaboration
- 用栈实现队列、用队列实现栈(C语言_leetcode_232+225)
- Research Report on the development trend and competitive strategy of the global camera filter bracket industry
- What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
- C语言课程设计题目
- 博文推荐 | 深入研究 Pulsar 中的消息分块
猜你喜欢

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

用对场景,事半功倍!TDengine 的窗口查询功能及使用场景全介绍

Logic is a good thing

2022-2-15 learning the imitation Niuke project - post in Section 2

leetcode622. Design cycle queue (C language)

TDengine 连接器上线 Google Data Studio 应用商店

Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its

QT learning management system

队列的基本操作(C语言实现)

佩服,阿里女程序卧底 500 多个黑产群……
随机推荐
Phpcms realizes the direct Alipay payment function of orders
Basis of target detection (NMS)
El form item regular verification
sqlilabs less9
开源实习经验分享:openEuler软件包加固测试
C语言课程设计题目
When the main process architecture game, to prevent calls everywhere to reduce coupling, how to open the interface to others to call?
【R语言数据科学】:机器学习常见评估指标
Logic is a good thing
Open source internship experience sharing: openeuler software package reinforcement test
User defined annotation realizes the function of verifying information
【IoT毕设.上】STM32+机智云AIoT+实验室安全监控系统
我们该如何保护自己的密码?
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
SWT/ANR问题--当发送ANR/SWT时候如何打开binder trace(BinderTraces)
Applet - multiple text line breaks in view
Summary of leetcode's dynamic programming 5
算网融合赋能行业转型,移动云点亮数智未来新路标
SWT / anr problem - how to open binder trace (bindertraces) when sending anr / SWT