알고리즘/프로그래머스
프로그래머스 - 야근 지수
시나모온
2020. 8. 15. 19:36
문제 링크입니다 :
우선 순위 큐 문제였다.
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
long long solution(int n, vector<int> works) {
long long answer = 0;
priority_queue<int> pq;
for(const auto work : works) {
pq.push(work);
}
while(n != 0) {
int first = pq.top();
pq.pop();
int second = pq.top();
if(first == 0) {
break;
}
if(first - second > 1) {
int minValue = min(n, first - second);
n -= minValue;
pq.push(first - minValue);
} else {
n--;
pq.push(first - 1);
}
}
while(!pq.empty()) {
int top = pq.top();
pq.pop();
answer += top * top;
}
return answer;
}
개발 환경 : vscode
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~