Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit106b814

Browse files
refactor 643
1 parentb440677 commit106b814

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
publicclass_643 {
1816

19-
publicstaticdoublefindMaxAverage(int[]nums,intk) {
20-
doublesum =0;
21-
doublemaxAve =Integer.MIN_VALUE;
22-
for (inti =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+
publicstaticclassSolution1 {
18+
publicdoublefindMaxAverage(int[]nums,intk) {
19+
doublesum =0;
20+
doublemaxAve =Integer.MIN_VALUE;
21+
for (inti =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+
returnmaxAve;
3031
}
31-
returnmaxAve;
32-
}
33-
34-
publicstaticvoidmain(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 =newint[]{-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-
intk =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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._643;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_643Test {
10+
privatestatic_643.Solution1solution1;
11+
privatestaticint[]nums;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_643.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
nums =newint[]{1,12, -5, -6,50,3};
21+
assertEquals(12.75,solution1.findMaxAverage(nums,4),0.01);
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
nums =newint[]{-1};
27+
assertEquals(-1.0,solution1.findMaxAverage(nums,1),0.01);
28+
}
29+
30+
@Test
31+
publicvoidtest3() {
32+
nums =newint[]{-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)

[8]ページ先頭

©2009-2025 Movatter.jp