forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d704afc
commit d525bfa
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.fishercoder.solutions; | ||
|
||
public class _1491 { | ||
public static class Solution1 { | ||
public double average(int[] salary) { | ||
int max = salary[0]; | ||
int min = salary[0]; | ||
for (int i = 1; i < salary.length; i++) { | ||
max = Math.max(max, salary[i]); | ||
min = Math.min(min, salary[i]); | ||
} | ||
long total = 0; | ||
int count = 0; | ||
for (int i = 0; i < salary.length; i++) { | ||
if (salary[i] != max && salary[i] != min) { | ||
total += salary[i]; | ||
count++; | ||
} | ||
} | ||
return (double) total / count; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.fishercoder; | ||
|
||
import com.fishercoder.solutions._1491; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
|
||
public class _1491Test { | ||
private static _1491.Solution1 solution1; | ||
private static int[] salary; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
solution1 = new _1491.Solution1(); | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
salary = new int[]{4000, 3000, 1000, 2000}; | ||
assertEquals(2500.0000, solution1.average(salary)); | ||
} | ||
|
||
} |