3. 과제 진행하기

aj4941·2023년 8월 30일
0

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

다양한 종류의 변수가 주어지고 큐를 관리해야 하기 때문에 tuple이나 pair로 관리하면 헷갈리기 쉬워서 구조체를 이용하여 변수를 알아보기 쉽게 관리하였다.
현재 작업중인 노드가 없을 경우 기본값을 dummy라는 값을 복사하여 넣어둔 점도 알아두자.
시간을 hh:mm 으로 주어진 것을 stringstream을 통해 파싱하는 과정도 알아둬야 한다.
중간에 24시를 넘어가면 더 계산할 필요 없이 sq에 있는 노드들을 하나씩 정답에 넣으면된다.

https://real-man-liljay.tistory.com/108 (다른 풀이)

#include <bits/stdc++.h>
using namespace std;

struct Node {
    string str;
    int t, rm;
};

bool cmp(Node &a, Node &b)
{
    return a.t < b.t;
}

Node dummy = { "-1", -1, -1 };

vector<string> solution(vector<vector<string>> plans)
{
    vector<Node> tmp;
    for (auto to : plans)
    {
        string r1 = to[0], r2 = to[1], r3 = to[2];
        stringstream sr(r2);
        string w;
        vector<string> v;
        while (getline(sr, w, ':'))
            v.push_back(w);
        
        string str = r1;
        int t = stoi(v[0]) * 60 + stoi(v[1]);
        int rm = stoi(r3);
        tmp.push_back({ str, t, rm });
    }
    
    sort(tmp.begin(), tmp.end(), cmp);
    deque<Node> q, sq;
    for (auto to : tmp) q.push_back(to);
    int t = 0;
    
    Node cur = dummy;
    vector<string> ans;
    
    while (t < 24 * 60)
    {
        if (cur.t != -1) // cur 존재
        {
            if (q.size() && t == q.front().t)
            {
                sq.push_front(cur);
                cur = q.front(); q.pop_front();
            }
        }
        else // cur 존재 X
        {
            if (q.size() && t == q.front().t)
                cur = q.front(), q.pop_front();
            else if (sq.size())
                cur = sq.front(), sq.pop_front();
        }
        
        if (cur.t != -1)
        {
            cur.rm--;
            if (cur.rm == 0)
            {
                ans.push_back(cur.str);
                cur = dummy;
            }
        }
        
        t++;
    }
    
    if (cur.t != -1)
        ans.push_back(cur.str);
    
    while (sq.size())
    {
        ans.push_back(sq.front().str);
        sq.pop_front();
    }
    
    return ans;
}
}```
profile
안녕하세요 aj4941 입니다.

0개의 댓글