File: average-salary-excluding-the-minimum-and-maximum-salary/solution.py

Date: 2026-06-06

Time: 15:18

Purpose

This file solves LeetCode 1491: Average Salary Excluding the Minimum and Maximum Salary. It computes the mean of a salary list after dropping the single smallest and single largest values. It follows the repo's convention of one Solution class per problem directory.

Key Components

Solution.average(salary: List[int]) -> float — The sole method. Takes a list of unique integer salaries (guaranteed length >= 3 by the problem constraints) and returns the average of all elements except the min and max.

Patterns

The solution uses a single-pass arithmetic approach rather than sorting. It computes sum(), min(), and max() — each an O(n) traversal — then derives the trimmed average algebraically:


(total - min - max) / (n - 2)

This avoids allocating a sorted copy (O(n log n)) and instead relies on three linear scans. Python's built-in functions make this both concise and readable.

Dependencies

Flow

1. Sum all salaries → total

2. Subtract min(salary) and max(salary) from total

3. Divide by len(salary) - 2 (the count of remaining elements)

4. Return the float result

No intermediate data structures, no mutation of the input.

Invariants

Error Handling

None. The method trusts LeetCode's input guarantees. No validation of list length, no check for duplicates, no guard against empty input. This is appropriate — LeetCode solutions operate within a well-defined contract enforced by the judge.

Topics to Explore

Beliefs