LeetCode #1491. Average Salary Excluding the Minimum and Maximum Salary
题目
1491. Average Salary Excluding the Minimum and Maximum Salary
解题方法
设置一个最小薪水和一个最大薪水,avg初始化为0,遍历数组累加avg,同时寻找最小和最大薪水,最后用avg减去两个薪水并除以剩余总数即可。
时间复杂度:O(n)
空间复杂度:O(1)
代码
class Solution:
def average(self, salary: List[int]) -> float:
minsalary = 1000000
maxsalary = 0
avg = 0
for i in salary:
avg += i
minsalary = min(minsalary, i)
maxsalary = max(maxsalary, i)
return (avg - minsalary - maxsalary) / (len(salary) - 2)