LeetCode - Algorithms - 1491. Average Salary Excluding the Minimum and Maximum Salary

Maybe this is the easiest problem on leetcode.

Problem

1491. Average Salary Excluding the Minimum and Maximum Salary

Java

\( O(N) \)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public double average(int[] salary) {
final int N = salary.length;
int min=salary[0],max=salary[0];
double sum = salary[0];
for (int i = 1; i < N; i++) {
if (salary[i]<min) min=salary[i];
if (salary[i]>max) max=salary[i];
sum += salary[i];
}
sum = sum - max - min;
return sum / (N - 2);
}
}

Submission Detail

  • 43 / 43 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Average Salary Excluding the Minimum and Maximum Salary.
  • Memory Usage: 37.1 MB, less than 11.62% of Java online submissions for Average Salary Excluding the Minimum and Maximum Salary.

\( O(NlogN) \)

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public double average(int[] salary) {
final int N = salary.length;
Arrays.sort(salary);
double t = salary[1];
for (int i = 2; i < N - 1; i++) {
t += salary[i];
}
return t / (N - 2);
}
}

Submission Detail

  • 43 / 43 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Average Salary Excluding the Minimum and Maximum Salary.
  • Memory Usage: 36.7 MB, less than 75.02% of Java online submissions for Average Salary Excluding the Minimum and Maximum Salary.