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.
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
난이도: easy
사원들 연봉의 평균을 구하는 문제이다
가장 높은 연봉을 받는 사원과 가장 낮은 연봉을 받는 사원을 제외한 나머지의 평균을 구해라
var average = function(salary) {
let result = 0;
const sorted = salary.sort((a, b) => a - b);
sorted.reduce((acc, cur) => {
result += cur;
})
result = result - sorted[sorted.length - 1]
return result / (salary.length - 2)
};
1. 정렬
2. 연봉 합치기 (여기서 인덱스를 빈칸으로 두어 가장 낮은 연봉은 제외하고 더해준다)
3. 가장 많은 연봉을 제외
4. 평균 구하기
'Algorithm' 카테고리의 다른 글
[Leetcode] 1523. Count Odd Number in an Interval Range (1) | 2022.03.17 |
---|---|
[Leetcode] 1137. N-th Tribonacci Number (0) | 2022.03.17 |
[Leetcode] 509. Fibonacci Number (0) | 2022.03.17 |
[Leetcode] 2176.Count Equal and Divisible Pairs in an Array (0) | 2022.03.12 |
[Leetcode] 21. Merge Two Sorted Lists (0) | 2022.03.12 |