Skip to content

Commit

Permalink
refactor 1491
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Jun 27, 2020
1 parent d704afc commit d525bfa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★

| # | Title | Solutions | Video | Difficulty | Tag
|-----|----------------|---------------|--------|-------------|-------------
|1491|[Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1491.java) | |Easy|Array, Sort|
|1487|[Making File Names Unique](https://leetcode.com/problems/making-file-names-unique/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1487.java) | |Medium|HashTable, String|
|1486|[XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1486.java) | |Medium|Array, Bit Manipulation|
|1481|[Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1481.java) | |Medium|Array, Sort|
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1491.java
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;
}
}
}
24 changes: 24 additions & 0 deletions src/test/java/com/fishercoder/_1491Test.java
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));
}

}

0 comments on commit d525bfa

Please sign in to comment.