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;
}
|