2018 KAKAO BLIND RECRUITMENT
셔틀버스를 타는데 가장 늦게 나가서 기다리는 시간을 구하는 문제입니다.
timetable을 정렬한 후 버스가 오는 시간에 맞춰서 차례대로 넣어줍니다.
마지막 버스가 왔을 때 모든 사람이 가득 찬 경우 - 마지막 사람 -1분을 출력합니다.
그 외에는 마지막 버스가 오는 시간을 출력해줍니다.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int convert(int time, int num, string type)
{
if(type == "plus"){
if((time % 100) + num >= 60){
time += 100;
time += (((time % 100) + num) % 60) - (time % 100);
} else{
time += num;
}
} else if(type == "minus"){
if((time % 100) - num < 0){
time -= 100;
time += (60 - (time % 100) - num) - (time % 100);
} else{
time -= num;
}
}
return time;
}
string solution(int n, int t, int m, vector<string> timetable) {
int one;
int ten;
int time = 900;
int index = -1;
int prevTime;
int prevPossible;
string strPrevTime;
string answer = "";
vector<int> tt;
for(int i=0; i<timetable.size(); ++i){
one = stoi(timetable[i].substr(3, 2));
ten = stoi(timetable[i].substr(0, 2));
tt.push_back((ten * 100) + one);
}
sort(tt.begin(), tt.end());
for(int i=0; i<n; ++i){
prevPossible = false;
for(int j=0; j<m; ++j){
++index;
if(time < tt[index] || index == tt.size()){
--index;
prevPossible = true;
break;
}
}
if(prevPossible){
prevTime = time;
} else {
prevTime = convert(tt[index], 1, "minus");
}
time = convert(time, t, "plus");
}
strPrevTime = to_string(prevTime);
if(strPrevTime.length() == 1){
answer += "00:0";
answer += strPrevTime[0];
}
else if(strPrevTime.length() == 2){
answer += "00:";
answer += strPrevTime[0];
answer += strPrevTime[1];
}
else if(strPrevTime.length() == 3){
answer += "0";
answer += strPrevTime[0];
answer += ":";
answer += strPrevTime[1];
answer += strPrevTime[2];
} else{
answer += strPrevTime[0];
answer += strPrevTime[1];
answer += ":";
answer += strPrevTime[2];
answer += strPrevTime[3];
}
return answer;
}
'알고리즘' 카테고리의 다른 글
[JavaScript] 프로그래머스 - 숫자 문자열과 영단어 (0) | 2021.09.14 |
---|---|
[C++] 프로그래머스 - 불량 사용자 (0) | 2021.09.14 |
[C++] 프로그래머스 - 거리두기 확인하기 (0) | 2021.09.14 |
[C++] 프로그래머스 - 추석 트래픽 (0) | 2021.09.14 |
[JavaScript] 프로그래머스 - 키패드 누르기 (0) | 2021.09.14 |