Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 106b814

Browse files
refactor 643
1 parent b440677 commit 106b814

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

src/main/java/com/fishercoder/solutions/_643.java

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,32 @@
22

33
/**
44
* 643. Maximum Average Subarray I
5-
*
65
* Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value.
76
* 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].
7+
* Example 1:
8+
* Input: [1,12,-5,-6,50,3], k = 4
9+
* Output: 12.75
10+
* Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
11+
* Note:
12+
* 1 <= k <= n <= 30,000.
13+
* Elements of the given array will be in the range [-10,000, 10,000].
1614
*/
1715
public class _643 {
1816

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);
17+
public static class Solution1 {
18+
public double findMaxAverage(int[] nums, int k) {
19+
double sum = 0;
20+
double maxAve = Integer.MIN_VALUE;
21+
for (int i = 0; i < nums.length; i++) {
22+
if (k <= i) {
23+
sum -= nums[i - k];
24+
}
25+
sum += nums[i];
26+
if ((i + 1) >= k) {
27+
maxAve = Math.max(maxAve, sum / k);
28+
}
2929
}
30+
return maxAve;
3031
}
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));
4432
}
4533
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._643;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _643Test {
10+
private static _643.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _643.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 12, -5, -6, 50, 3};
21+
assertEquals(12.75, solution1.findMaxAverage(nums, 4), 0.01);
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{-1};
27+
assertEquals(-1.0, solution1.findMaxAverage(nums, 1), 0.01);
28+
}
29+
30+
@Test
31+
public void test3() {
32+
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};
33+
assertEquals(37.25555555555555, solution1.findMaxAverage(nums, 90), 0.01);
34+
}
35+
}

0 commit comments

Comments
 (0)