포스트

Hackerrank Jesse and Cookies

Two Strings

문제링크

설명

우선순위 큐를 활용하는 문제이다. 요소의 순위를 계속 유지해야하므로 이럴 때에는 우선순위 큐를 이용하자.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//PriorityQueue가 구현되어있는 java를 활용함
static int cookies(int k, int[] A) {

    int calCount = 0;
    PriorityQueue<Integer> cookies = new PriorityQueue<Integer>();

    for(int i = 0; i < A.length; i++){
        cookies.offer(A[i]);
    }

    //주어진 조건을 만족하여 연산이 필요없는경우
    if(cookies.peek() >= k)
    {
        return calCount;
    }

    while(cookies.size() > 1 && cookies.peek() < k){
        calCount++;

        int leastSweetCookie = cookies.poll();
        int secondSweetCookie = cookies.poll();
        int combinedCookie = 1 * leastSweetCookie + 2 * secondSweetCookie;

        cookies.offer(combinedCookie);

        if(cookies.peek() >= k){
            return calCount;
        }
    }
    return -1;
}

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.