Average Salary Excluding the Minimum and Maximum Salary — LeetCode #1491 (Easy)
Tags: Array, Sorting
Related patterns: Array, Mathematical Aggregation
Brute Force approach
Time complexity: O(n²). Space complexity: O(1).
The brute-force approach involves calculating the sum of all salaries, finding the minimum and maximum salaries, and then computing the average of the remaining salaries. This method is straightforward but not the most efficient.
The time complexity is O(n²) because we are iterating through the array multiple times (once for sum, once for min, and once for max). However, in this case, we can achieve it in a single pass, so we can improve this.
- Step 1: Initialize a variable to hold the total sum of salaries.
- Step 2: Iterate through the salary array to find the minimum and maximum salaries while calculating the total sum.
- Step 3: Subtract the minimum and maximum salaries from the total sum.
- Step 4: Calculate the average by dividing the result by the number of salaries minus 2.
1. Input: salary = [4000, 3000, 1000, 2000]
2. total_sum = 0, min_salary = inf, max_salary = -inf
3. Iterating through salary:
- Add 4000: total_sum = 4000, min_salary = 4000, max_salary = 4000
- Add 3000: total_sum = 7000, min_salary = 3000, max_salary = 4000
- Add 1000: total_sum = 8000, min_salary = 1000, max_salary = 4000
- Add 2000: total_sum = 10000, min_salary = 1000, max_salary = 4000
4. total_sum = 10000, min_salary = 1000, max_salary = 4000
5. average = (10000 - 1000 - 4000) / (4 - 2) = 2500.0
Optimal Solution approach
Time complexity: O(n). Space complexity: O(1).
The optimal solution improves on the brute-force approach by calculating the total sum in a single pass while simultaneously finding the minimum and maximum salaries. This reduces the number of iterations needed.
The time complexity is O(n) because we only loop through the array once to calculate the total sum, minimum, and maximum salaries. The space complexity is O(1) since we are using a constant amount of extra space.
- Step 1: Initialize variables for total sum, minimum salary, and maximum salary.
- Step 2: Iterate through the salary array to compute total sum, minimum, and maximum in one pass.
- Step 3: Subtract the minimum and maximum from the total sum.
- Step 4: Divide the result by the number of salaries minus 2 to get the average.
1. Input: salary = [4000, 3000, 1000, 2000]
2. total_sum = 0, min_salary = inf, max_salary = -inf
3. Iterating through salary:
- Add 4000: total_sum = 4000, min_salary = 4000, max_salary = 4000
- Add 3000: total_sum = 7000, min_salary = 3000, max_salary = 4000
- Add 1000: total_sum = 8000, min_salary = 1000, max_salary = 4000
- Add 2000: total_sum = 10000, min_salary = 1000, max_salary = 4000
4. total_sum = 10000, min_salary = 1000, max_salary = 4000
5. average = (10000 - 1000 - 4000) / (4 - 2) = 2500.0
Key Insights
- Understanding how to efficiently calculate aggregates (sum, min, max) in a single pass can significantly improve performance.
- Recognizing that the constraints guarantee unique salaries simplifies the problem, as we don't need to handle duplicates.
Common Mistakes
- Failing to account for the correct number of elements when calculating the average (subtracting 2 from the total count).
- Not considering edge cases, such as the minimum number of salaries required to compute the average.
Interview Tips
- Always clarify the problem requirements and constraints before jumping into coding.
- Think aloud during the interview to show your thought process and reasoning.
- Practice dry runs of your code with sample inputs to ensure correctness before finalizing your solution.