# 【LeetCode】 1491. Average Salary Excluding the Minimum and Maximum Salary
## Description
> 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.
> Constraints:
> * `3 <= salary.length <= 100`
> * `1000 <= salary[i] <= 10^6`
> * All the integers of `salary` are unique.
> 給你一個只包含唯一整數的陣列 `salary`,其中 `salary[i]` 是第 `ith` 員工的薪水。
> 回傳除了最高薪與最低薪以外每個員工的平均薪水。答案在誤差小於 `10^-5` 之下都會被接受。
> 限制:
> * `3 <= salary.length <= 100`
> * `1000 <= salary[i] <= 10^6`
> * 所有 `salary` 裡面的整數都是唯一的。
## Example:
```
Example 1:
Input: salary = [4000,3000,1000,2000]
Output: 2500.00000
Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively.
Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500
```
```
Example 2:
Input: salary = [1000,2000,3000]
Output: 2000.00000
Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively.
Average salary excluding minimum and maximum salary is (2000) / 1 = 2000
```
## Solution
* 題目可以拆成兩個部分
* 得到最大最小薪水
* 得到總和(用於平均)
* 而這兩個任務都可以在一次遍歷中求得,因此可用一個 for loop 完成
### Code
```C++=1
class Solution {
public:
double average(vector<int>& salary) {
int max_s = 0, min_s = 1000001;
double sum = 0;
for(int i = 0; i < salary.size(); i++)
{
sum += salary[i];
max_s = max(max_s, salary[i]);
min_s = min(min_s, salary[i]);
}
sum = (sum - max_s - min_s) / (salary.size() - 2);
return sum;
}
};
```
###### tags: `LeetCode` `C++`