[Programmers] 라면공장 - 힙(Heap)

동민·2021년 3월 11일
import java.util.Collections;
import java.util.PriorityQueue;

// 라면공장 - 힙(Heap)
public class RamenFactory {
	public int solution(int stock, int[] dates, int[] supplies, int k) {
		int answer = 0, d = 0, i = 0;
		PriorityQueue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());

		while (d <= k - 1) {
			stock--;
			d++;

			if (i < dates.length && d == dates[i]) { // 날짜를 하루하루 카운트 하면서 dates에 들어있는 날짜가 되면 힙에 offer
				heap.offer(supplies[i]);
				i++;
			}
			if (stock == 0 && d != k) { // 날짜를 하루하루 카운트 하면서 stock이 바닥날 때 max heap 에서 poll
				stock += heap.poll();
				answer++;
			}
		}
		return answer;
	}
}
profile
BE Developer

0개의 댓글