-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (27 loc) · 889 Bytes
/
Solution.cs
File metadata and controls
29 lines (27 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace LeetCode.Problem643.Alternative{
//643. Maximum Average Subarray I
//https://leetcode.com/problems/maximum-average-subarray-i/
/*
You are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value.
Any answer with a calculation error less than 10-5 will be accepted.
*/
public class Solution {
public double FindMaxAverage(int[] nums, int k) {
int left = 0;
int right = 0;
double average = double.MinValue;
int Sum = 0;
while (right < nums.Length)
{
while (right - left < k )
{
Sum += nums[right++];
}
average = Math.Max(average, (double)Sum / k);
Sum -= nums[left++];
}
return average;
}
}
}