알고리즘 풀이/프로그래머스

프로그래머스 - 라면공장 (힙)

배게 2019. 12. 4. 09:54
728x90
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
33
34
35
36
    public int solution(int stock, int[] datesInput, int[] suppliesInput, int k) {
        int answer = 0;
 
        Queue<Integer> dates = new LinkedList<>();
        Queue<Integer> supplies = new LinkedList<>();
 
        for (int d : datesInput) {
            // System.out.println(d);
            dates.offer(d);
        }
        System.out.println();
        for (int s : suppliesInput) {
            // System.out.println(s);
            supplies.offer(s);
        }
 
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        int day = 0;
        while (day < k) {
 
            if (!dates.isEmpty() && dates.peek() == day) {
                dates.poll();
                pq.add(supplies.poll());
            }
 
            if (stock == 0) {
                answer++;
                stock += pq.poll();
            }
            stock--;
            day++;
 
        }
 
        return answer;
    }
cs