-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMaximum Average Subarray I.java
executable file
·52 lines (46 loc) · 1.31 KB
/
Maximum Average Subarray I.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
E
1517552169
tags: Array, Subarray
time: O(n)
space: O(1)
简单的求sum of fixed window k, 同时求max avg, 结尾求余数就好.
```
/*
Given an array consisting of n integers, find the contiguous subarray of given length k
that has the maximum average value. And you need to output the maximum average value.
Example 1:
Input: [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
Note:
1 <= k <= n <= 30,000.
Elements of the given array will be in the range [-10,000, 10,000].
*/
/*
Thoughts:
Given a k as range, find the max sum.
Brutle: use the box, calculate sum one index at a time, and find the max.
No need to add 4 numbers all the time, just minus the leading one and add the new one.
Note: use double to catch the sum.
*/
class Solution {
public double findMaxAverage(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return 0;
}
double sum = 0;
// init the sum of k items
for (int i = 0; i < k; i++) {
sum += nums[i];
}
// calculate
double max = sum;
for (int i = k; i < nums.length; i++) {
max = Math.max(sum, max);
sum = sum - nums[i - k] + nums[i];
}
max = Math.max(sum, max);
return max / k;
}
}
```