Skip to content

Commit 176473f

Browse files
add 643
1 parent bea8e5e commit 176473f

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|643|[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_643.java) | O(n) |O(1) | Easy |
2324
|640|[Solve the Equation](https://leetcode.com/problems/solve-the-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_640.java) | O(n) |O(n) | Medium |
2425
|638|[Shopping Offers](https://leetcode.com/problems/shopping-offers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_638.java) | O(2^n) |O(n) | Medium | DP, DFS
2526
|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_637.java) | O(n) |O(1) | Easy |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 643. Maximum Average Subarray I
5+
*
6+
* Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value.
7+
* And you need to output the maximum average value.
8+
9+
Example 1:
10+
Input: [1,12,-5,-6,50,3], k = 4
11+
Output: 12.75
12+
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
13+
Note:
14+
1 <= k <= n <= 30,000.
15+
Elements of the given array will be in the range [-10,000, 10,000].
16+
*/
17+
public class _643 {
18+
19+
public static double findMaxAverage(int[] nums, int k) {
20+
double sum = 0;
21+
double maxAve = Integer.MIN_VALUE;
22+
for (int i = 0; i < nums.length; i++) {
23+
if (k <= i) {
24+
sum -= nums[i-k];
25+
}
26+
sum += nums[i];
27+
if ((i+1) >= k) {
28+
maxAve = Math.max(maxAve, sum / k);
29+
}
30+
}
31+
return maxAve;
32+
}
33+
34+
public static void main(String... args) {
35+
// int[] nums = new int[]{1,12,-5,-6,50,3};
36+
// int k = 4;
37+
38+
// int[] nums = new int[]{-1};
39+
// int k = 1;
40+
41+
int[] nums = new int[]{-6662,5432,-8558,-8935,8731,-3083,4115,9931,-4006,-3284,-3024,1714,-2825,-2374,-2750,-959,6516,9356,8040,-2169,-9490,-3068,6299,7823,-9767,5751,-7897,6680,-1293,-3486,-6785,6337,-9158,-4183,6240,-2846,-2588,-5458,-9576,-1501,-908,-5477,7596,-8863,-4088,7922,8231,-4928,7636,-3994,-243,-1327,8425,-3468,-4218,-364,4257,5690,1035,6217,8880,4127,-6299,-1831,2854,-4498,-6983,-677,2216,-1938,3348,4099,3591,9076,942,4571,-4200,7271,-6920,-1886,662,7844,3658,-6562,-2106,-296,-3280,8909,-8352,-9413,3513,1352,-8825};
42+
int k = 90;
43+
System.out.println(findMaxAverage(nums, k));
44+
}
45+
}

0 commit comments

Comments
 (0)