Skip to content

1491. Average Salary Excluding the Minimum and Maximum Salary 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  double average(vector<int>& salary) {
    const double sum = accumulate(salary.begin(), salary.end(), 0.0);
    const int mx = ranges::max(salary);
    const int mn = ranges::min(salary);
    return (sum - mx - mn) / (salary.size() - 2);
  }
};
1
2
3
4
5
6
7
8
class Solution {
  public double average(int[] salary) {
    final double sum = Arrays.stream(salary).sum();
    final int mx = Arrays.stream(salary).max().getAsInt();
    final int mn = Arrays.stream(salary).min().getAsInt();
    return (sum - mx - mn) / (salary.length - 2);
  }
}
1
2
3
class Solution:
  def average(self, salary: list[int]) -> float:
    return (sum(salary) - max(salary) - min(salary)) / (len(salary) - 2)