0%

[筆記] Leetcode - 1491. Average Salary Excluding the Minimum and Maximum Salary

https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/

You are given an array of unique integers salary where salary[i] is the salary of the ith employee.
Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted.

1
2
3
4
class Solution:
def average(self, salary: List[int]) -> float:
salary.sort()
return sum(salary[1:-1]) / (len(salary) - 2)
1
2
3
4
def average(self, salary: List[int]) -> float:
salary.remove(min(salary))
salary.remove(max(salary))
return sum(salary) / len(salary)
1
2
def average(self, salary: List[int]) -> float:
return (sum(salary) - min(salary) - max(salary)) / (len(salary) - 2)
1
2
3
4
5
def average(self, salary: List[int]) -> float:
salary.sort()
salary.pop(0)
salary.pop()
return sum(salary) / len(salary)