호텔을 운영 중인 코니는 최소한의 객실만을 사용하여 예약 손님들을 받으려고 합니다. 한 번 사용한 객실은 퇴실 시간을 기준으로 10분간 청소를 하고 다음 손님들이 사용할 수 있습니다.
예약 시각이 문자열 형태로 담긴 2차원 배열 book_time이 매개변수로 주어질 때, 코니에게 필요한 최소 객실의 수를 return 하는 solution 함수를 완성해주세요.
#include <string> #include <vector> #include <iostream> #include <queue> #include <algorithm> using namespace std; bool compare(pair<int, int> a, pair<int, int> b) { if(a.first == b.first) { // 시작 시간이 같으면 return a.second < b.second; // 종료 시간이 빠른 순 } return a.first < b.first; // 시작 시간이 빠른 순서대로 } int solution(vector<vector<string>> book_time) { int answer = 0; vector <pair<int, int>> list(book_time.size()); for(int i=0; i<book_time.size(); i++) { int h = stoi(book_time[i][0].substr(0, 2)); int m = stoi(book_time[i][0].substr(3, 2)); list[i].first = h * 60 + m; h = stoi(book_time[i][1].substr(0, 2)); m = stoi(book_time[i][1].substr(3, 2)); list[i].second = h * 60 + m + 10; } sort(list.begin(), list.end(), compare); queue <int> tmp; int idx = 0, time = 23 * 60 + 59; for(int i=0; i<=time; i++) { if(idx >= book_time.size()) break; while(list[idx].first == i) { tmp.push(list[idx].second); idx++; } int n = tmp.size(); while(n--) { if(tmp.front() == i) { tmp.pop(); } else { tmp.push(tmp.front()); tmp.pop(); } } if(answer < tmp.size()) { answer = tmp.size(); } } return answer; }