[프로그래머스(Programmers)] 야근 지수 (java)

2
post-thumbnail

안녕하세요.! 오늘은 프로그래머스의 야근 지수 문제를 풀어보겠습니다.😊


문제 링크

https://programmers.co.kr/learn/courses/30/lessons/12927

전체 코드

import java.util.*;

class Solution {
    public long solution(int n, int[] works) {
        long sum = 0;
        PriorityQueue<Integer> pQueue = new PriorityQueue(Collections.reverseOrder());
        
        for(int num : works) {
            pQueue.offer(num);
        }
        
        for(int i=0; i<n; i++) {
            int max = pQueue.poll();
            if(max <= 0) break;
            pQueue.offer(max-1);
        }
        
        while(!pQueue.isEmpty()) {
            sum += Math.pow(pQueue.poll(), 2);
        }
        
        return sum;
    }
}

느낀점

초기 착안을 잘못했지 않았나 싶다. 수학적으로 접근해야하는 줄 알고 나누고 더하고 엄청 했는데 알고보니 우선순위큐 이용해서 가장 큰 값을 가진 큐에서 하나하나씩 빼주면 되는거였다.. 풀듯 말듯 안풀려서 이틀을 고민했는데 결국 못풀고 답을 봐서 아쉬움이 남는다. 어쨌든 우선순위 큐의 성질에 대해서 다시 한 번 짚고 넘어갈 수 있었던 좋은 문제였다.


[참고한 곳]
https://mapled.tistory.com/entry/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A43%EB%8B%A8%EA%B3%84%EC%9E%90%EB%B0%94-%EC%95%BC%EA%B7%BC-%EC%A7%80%EC%88%98-%EC%A0%95%ED%99%95%EC%84%B1-O-%ED%9A%A8%EC%9C%A8%EC%84%B1-O

0개의 댓글