본문 바로가기
Algorithm/Mastered

[LeetCode] 1491. Average Salary Excluding the Minimum and Maximum Salary

by Baley 2022. 9. 18.

LeetCode: 1491. 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.

주어진 배열에서 최댓값과 최소값을 제외하고 평균을 구하는 문제이다.

성능을 생각해 max와 min, sum 함수를 사용하지 않고 풀어보았다.

def average(self, salary: List[int]) -> float:
    maxi = 1000
    mini = 10**6
    tot = 0

    for i in salary:
        if i > maxi:
            maxi = i
        if i < mini:
            mini = i
        tot += i

    return (tot - maxi - mini)/ (len(salary)-2)

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

댓글