강의로 돌아가기
김효중

4,5,6,10,20번 테케가 틀립니다. 반례나 제 코드를 지적해주세요..

정렬 + 스택을 이용해서 풀이하였는데 4,5,6,10,20 테케가 틀리네요..지적해 주시면 감사하겠습니다!🤐

작성중인 코드―solution.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std;


int parseTime(string str){
    int index = 0;
    for(int i = 0;i<str.size();i++){
        if(str[i] == ':'){
            index = i;
            break;
        }
    }


    int Hour = stoi(str.substr(0,index));
    int Minute = stoi(str.substr(index+1,str.size()));
    return (Hour * 60) + Minute;

}

//[00:00(시작시간) , playtime]이 주어졌을 때 총 얼마만큼의 시간이 걸리는지 계산
int timeofValue(string startTime,string duration){
    int val = 0;
    int index = 0;
    for(int i = 0;i<startTime.size();i++){
        if(startTime[i] == ':'){
            index = i;
            break;
        }
    }
    //시작 시간의 시
    int Hour = stoi(startTime.substr(0,index));
    //시작 시간의 분
    int Minute = stoi(startTime.substr(index+1,startTime.size()));


    //지속시간의 시간
    int DurationHour = stoi(duration) / 60;
    //지속시간의 분
    int DurationMinute = stoi(duration) % 60;

    //시작시간의 시 + 지속시간의 시
    Hour += DurationHour;
    //시작 시간의 분 + 지속시간의 분을 더해서 60을 넘어가면 시 갱신
    if(Minute + DurationMinute >= 60){
        int updateHour = (Minute + DurationMinute) / 60;
        int updateMinute = (Minute + DurationMinute) % 60;
        Hour += updateHour;
        Minute = updateMinute;
    }
    //그렇지 않으면 두 시간의 분을 더해서
    else{
        Minute += DurationMinute;
    }
    //최종 걸리는 시간 반환
    return (Hour * 60) + Minute;
}

bool cmp(vector<string> a,vector<string> b){
    return a[1] < b[1];
}


vector<string> solution(vector<vector<string>> plans) {
    vector<string> answer;
    //먼저 시작 시간을 오름차순으로 정렬
    sort(plans.begin(),plans.end(),cmp);

    //{{과제이름,시작시간,지속시간을 더한 총 시간},지속시간}을 스택에 넣어둠
    stack<pair<pair<string,int>,int>> st;


    for(int i = 0;i<plans.size();i++){
        //스택이 비어있을 떄는 그냥 넣고
        if(st.empty()){
            st.push({{plans[i][0],timeofValue(plans[i][1],plans[i][2])},stoi(plans[i][2])});
        }
        // 그외의 경우
        else{


            //기존 과제를 끝내는 경우

                int Time = parseTime(plans[i][1]);
                while(1){
                    if(st.empty() || st.top().first.second > Time){
                        break;
                    }
                    Time -= st.top().second;
                    answer.push_back(st.top().first.first);
                    st.pop();
                }
                st.push({{plans[i][0],timeofValue(plans[i][1],plans[i][2])},stoi(plans[i][2])});

        }
    }

    while(!st.empty()){
        answer.push_back(st.top().first.first);
        st.pop();
    }
    return answer;
}
1 개의 답변
서진우

잘은 모르겠는데 {{"1", "00:00", "30"}, {"2", "00:10", "10"}, {"3", "00:30", "10"}, {"4", "00:50", "10"}} 넣어보실래요?
2314가 나와야 하거든요? 근데 2341 나오실 것 같네요.
제 예상대로 나온 거면 남은 과제 시간 갱신을 해주는 부분이 없는 게 문제로 보이네요.

답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.