티스토리 뷰
문제 링크입니다 : programmers.co.kr/learn/courses/30/lessons/42891
코딩테스트 연습 - 무지의 먹방 라이브
programmers.co.kr
우선순위 큐 + 구현 문제였다. 구현 문제는 실수 안 하기가 진짜 어려운것 같다. 실수 하나를 해서 10분정도 시간을 더 쓴것 같다.
#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(vector<int> food_times, long long k) {
int answer = 0;
int step = 0;
vector<bool> eatAll(food_times.size(), false);
priority_queue<pair<int, int>> pq;
for(int i = 0; i < food_times.size(); i++) {
pq.push({-food_times[i], i});
}
while(true) {
while(true) {
if(pq.empty()) return -1;
if(-pq.top().first <= step) {
eatAll[pq.top().second] = true;
pq.pop();
} else {
break;
}
}
int pqSize = pq.size();
if(k >= pqSize) {
k -= pqSize;
step++;
} else {
for(int i = 0; i < eatAll.size(); i++) {
if(!eatAll[i]) {
if(k == 0) {
answer = i + 1;
break;
}
k--;
}
}
break;
}
}
return answer;
}

개발 환경 : vscode
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스 - JadenCase 문자열 만들기 (0) | 2020.09.24 |
---|---|
프로그래머스 - 경주로 건설 (0) | 2020.09.12 |
프로그래머스 - 후보키 (0) | 2020.09.10 |
프로그래머스 - 실패율 (0) | 2020.09.09 |
프로그래머스 - 오픈채팅방 (0) | 2020.09.09 |