题目链接:1491. 去掉最低工资和最高工资后的工资平均值 - 力扣(LeetCode)

题目描述:

找到最大值最小值的位置,除了这两个,其他所有数求和然后求平均值。时间复杂度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
double average(vector<int>& salary) {
int min_loc = std::min_element(salary.begin(), salary.end()) - salary.begin();
int max_loc = std::max_element(salary.begin(), salary.end()) - salary.begin();
int sum = 0;
for (int i = 0; i < salary.size(); i ++) {
if (i == min_loc || i == max_loc)
continue;
sum += salary[i];
}
return 1. * sum / (salary.size() - 2);
}
};

评测结果: