Average Salary Excluding the Minimum and Maximum Salary — LeetCode 1491 Python Solution

EasyArraySorting
Problem
#1491
Pattern
Sorting
Reading time
2 min

The problem

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.

Example

Input
salary = [4000,3000,1000,2000]
Output
2500.00000
Explanation
Minimum salary and maximum salary are 1000 and 4000 respectively.

Python solution

Python
class Solution:
    def average(self, salary: List[int]) -> float:
        s = sum(salary) - min(salary) - max(salary)
        return s / (len(salary) - 2)

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array `salary`
SpaceO(1) auxiliary

Pattern: Sorting

Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.

The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary?
LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary is rated Easy on LeetCode.
What is the time complexity of LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary?
The Python solution on this page runs in O(n), where n is the length of the array `salary`.
What is the space complexity of LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary cover?
LeetCode 1491. Average Salary Excluding the Minimum and Maximum Salary is tagged Array and Sorting on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview