Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 세그먼트 트리
- CS
- 백트래킹
- 스택
- 이분탐색
- BFS
- dfs
- 알고리즘
- java
- 완전탐색
- 수학
- 백준
- swea
- Network
- 플로이드-와샬
- 유니온 파인드
- 투 포인터
- 구현
- 문자열
- JUnit 5
- 시뮬레이션
- Kotlin
- 프로그래머스
- 에라토스테네스의 체
- Effective Java
- 후니의 쉽게 쓴 시스코 네트워킹
- 그리디
- 동적계획법
- mst
- 위상정렬
Archives
반갑습니다!
[프로그래머스] 라면공장 본문
풀이
Priority_Queue
를 사용해서 밀가루가 0이 될 때마다 가장 많은 양을 보충해주면 된다.
코드
C++
#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(int stock, vector<int> dates, vector<int> supplies, int k) {
int answer = 0;
int day = 0;
int idx = 0;
priority_queue<int> pq;
while (day < k) {
// 공급받을 수 있는 날이 되면 pq에 저장
if (idx < dates.size() && day == dates[idx]) {
pq.push(supplies[idx]);
idx++;
}
// 남은 밀가루가 없으면 가장 많은 양 공급받음
if (stock == 0) {
stock += pq.top();
pq.pop();
answer++;
}
stock--;
day++;
}
return answer;
}
Java
import java.util.Collections;
import java.util.PriorityQueue;
class Solution {
public int solution(int stock, int[] dates, int[] supplies, int k) {
int answer = 0;
int day = 0;
int idx = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
while(day < k){
if(idx < dates.length && day == dates[idx]){
pq.add(supplies[idx]);
idx++;
}
if(stock == 0){
stock += pq.poll();
answer++;
}
stock--;
day++;
}
return answer;
}
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[프로그래머스] 자물쇠와 열쇠 (0) | 2020.04.12 |
---|---|
[백준] 2638 치즈 (0) | 2020.04.11 |
[프로그래머스] 등굣길 (0) | 2020.04.09 |
[프로그래머스] 정수 삼각형 (0) | 2020.04.09 |
[프로그래머스] 땅따먹기 (0) | 2020.04.09 |