넘치게 채우기

[LeetCode] 1491. Average Salary Excluding the Minimum and Maximum Salary 본문

PS/LeetCode

[LeetCode] 1491. Average Salary Excluding the Minimum and Maximum Salary

riveroverflow 2023. 5. 1. 11:14
728x90
반응형

https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/description/

Average Salary Excluding the Minimum and Maximum Salary - LeetCode

Can you solve this real interview question? Average Salary Excluding the Minimum and Maximum Salary - 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 m

leetcode.com

문제 유형 : 정렬
난이도 : Easy
 
문제
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.
 
입력
봉급의 배열 salary가 주어진다.
3 <= salary.length <= 100
1000 <= salary[i] <= 10^6
 
출력
최소와 최대값을 제외한 평균 봉급을 리턴한다.
 
풀이
기본 라이브러리에서 제공하는 정렬을 하고, 첫 번째 인덱스와 맨 끝 인덱스를 제외하고 평균을 구해주면 된다.
그러나 나는 우선순위 큐를 이용해보았다.
 
코드(C++)

class Solution {
public:
    double average(vector<int>& salary) {
        double sum = 0;
        priority_queue<int> q;
        for (int i = 0; i < salary.size(); i++){
            q.push(salary.at(i));
        }
        q.pop();
        for (int i = 0; i < salary.size()-2; i++){
            sum += q.top();
            q.pop();
        }
        return sum / (salary.size()-2);


    }
};

 

728x90
반응형