Leetcode Problem 1491. Average Salary Excluding the Minimum and Maximum Salary

1491. Average Salary Excluding the Minimum and Maximum Salary

Leetcode Solutions

Key approach of the solution

  1. Initialize minSalary to float('inf') (infinity) and maxSalary to -float('inf') (negative infinity).
  2. Initialize sum to 0.
  3. Iterate over each salary in the salaries array.
    • Add salary to sum.
    • Update minSalary if salary is less than minSalary.
    • Update maxSalary if salary is greater than maxSalary.
  4. Subtract minSalary and maxSalary from sum to get the sum of the remaining salaries.
  5. Divide the adjusted sum by the number of salaries minus 2 to get the average.
  6. Return the average.
UML Thumbnail

Alternative approach using sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...