6. 호텔 대실

aj4941·2023년 8월 30일
0

https://school.programmers.co.kr/learn/courses/30/lessons/155651

시간을 파싱하는 과정을 따로 함수를 둔 것이 solution 함수를 보기 더 편하게 만들었다.
구현이 복잡하다고 느껴지는 부분들을 따로 함수로 빼서 관리하면 디버깅에서도 유용할 것이라는 생각이 많이 들었다.

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

int calc(string s)
{
    stringstream sr(s);
    string w;
    vector<string> v;
    while (getline(sr, w, ':'))
        v.push_back(w);
    
    return stoi(v[0]) * 60 + stoi(v[1]);
}

int solution(vector<vector<string>> s) 
{
    vector<pii> tmp;
    
    for (auto to : s)
    {
        string r1 = to[0], r2 = to[1];
        int st = calc(r1), ed = calc(r2) + 10;
        tmp.push_back({ st, ed });
    }
    
    sort(tmp.begin(), tmp.end());
    priority_queue<int> pq;
    
    int ans = 0;
    
    for (int i = 0; i < tmp.size(); i++)
    {
        int st = tmp[i].first, ed = tmp[i].second;
        if (pq.size() == 0)
        {
            pq.push(-ed);
            ans = 1;
        }
        else
        {
            int mn = -pq.top();
            if (mn <= st) pq.pop();
            pq.push(-ed);
            ans = max(ans, (int)pq.size());
        }
    }
    
    return ans;
}
profile
안녕하세요 aj4941 입니다.

0개의 댓글