ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] 프로그래머스 과제 진행하기
    카테고리 없음 2023. 4. 1. 14:48
    반응형
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    #include <string>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #include <iostream>
    #include <deque>
     
    using namespace std;
     
    bool cmp(vector<string> a, vector<string> b) {                                                        
        return a[1< b[1];
    }
     
    int timediff(string a, string b) {
        string first_hour = "";
        first_hour = first_hour + a[0+ a[1];
        int first = stoi(first_hour) * 60;
        string first_minute = "";
        first_minute = first_minute + a[3+ a[4];
        first += stoi(first_minute);
        
        string second_hour = "";
        second_hour = second_hour + b[0+ b[1];
        int second = stoi(second_hour) * 60;
        string second_minute = "";
        second_minute = second_minute + b[3+ b[4];
        second += stoi(second_minute);
        
        return second - first;
    }
     
    vector<string> solution(vector<vector<string>> plans) {
        vector<string> answer;
        deque<pair<string,int>> undone;
        
        sort(plans.begin(),plans.end(),cmp);
        
        for(int i=0; i<plans.size()-1; i++) {
            int minus = timediff(plans[i][1],plans[i+1][1]);
            if(minus >= stoi(plans[i][2])) {
                answer.push_back(plans[i][0]);
                int time_tmp = minus - stoi(plans[i][2]);
                
                while(!undone.empty()) {
                    int rest_time = undone[0].second;
                
                    if(rest_time <= time_tmp) {
                        answer.push_back(undone[0].first);
                        undone.pop_front();
                        time_tmp -= rest_time;
                    }
                    else {
                        undone[0].second -= time_tmp;
                        break;
                    }
                }
            }
            else {
                int tmp = stoi(plans[i][2]) - minus;
                undone.push_front({plans[i][0], tmp});
            }
        }
        answer.push_back(plans[plans.size()-1][0]);
            
        while(!undone.empty()) {
            answer.push_back(undone[0].first);
            undone.pop_front();
        }
        
        return answer;
    }
    cs

     

     

    1. 시간 순으로 정렬

     

    2. 정렬된 상태에서 현재 과제의 시작시간과 다음 과제의 시작 시간을 비교 

     

    3. 비교 시간이 현재 과제의 진행 시간보다 짧을 경우 

    { 과제이름, 현재 과제의 진행시간 - 비교시간 } 을 덱에 저장

     

    4. 비교 시간이 현재 과제의 진행 시간보다 길 경우

    4-1. 현재 과제명을 answer 벡터에 저장

    4-2. 덱에 저장된 첫번째 인덱스부터 과제 진행

     

    5. 덱에 남은 과제를 첫번째 인덱스 순서대로 answer 벡터에 넣어줌

    728x90
    반응형

    댓글

Designed by Tistory.