https://programmers.co.kr/learn/courses/30/lessons/42629
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
힙을 활용하는 문제입니다.
아래는 정답코드입니다.
먼저 날짜내에 들어갈 수 있는 supplies를 우선순위 큐에 집어 넣어 놓습니다.
큐에서는 가장 큰 수 부터 꺼낼 수 있도록 맨 앞에 맥스힙으로 먼저 변경을 해 놓았습니다.
그렇게 되면 조건을 만족할때까지만 큐에서 값을 꺼내게 되므로 정답을 구할 수 있습니다.
정답코드
import java.util.*;
class Solution {
public int solution(int stock, int[] dates, int[] supplies, int k) {
int answer = 0;
int total_stock = stock;
int idx = 0;
PriorityQueue<Integer> priorityQueue = new PriorityQueue(Comparator.reverseOrder());
while(total_stock < k){
while(idx <dates.length && dates[idx] <= total_stock){
priorityQueue.offer(supplies[idx++]);
}
total_stock += priorityQueue.poll();
answer++;
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 - 구명보트 (0) | 2020.04.17 |
---|---|
프로그래머스 - 큰 수 만들기 (0) | 2020.04.17 |
프로그래머스 - 조이스틱(오답) (0) | 2020.04.12 |
프로그래머스 - H - index (0) | 2020.04.12 |
프로그래머스 - k번째수 (0) | 2020.04.12 |