diff --git a/README.md b/README.md index eae0590af2..2563affb05 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/src/main/java/com/fishercoder/solutions/_1491.java b/src/main/java/com/fishercoder/solutions/_1491.java new file mode 100644 index 0000000000..cf52b49b21 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_1491.java @@ -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; + } + } +} diff --git a/src/test/java/com/fishercoder/_1491Test.java b/src/test/java/com/fishercoder/_1491Test.java new file mode 100644 index 0000000000..34e4652568 --- /dev/null +++ b/src/test/java/com/fishercoder/_1491Test.java @@ -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)); + } + +} \ No newline at end of file