https://programmers.co.kr/learn/courses/30/lessons/17678
코딩테스트 연습 - [1차] 셔틀버스
10 60 45 ["23:59","23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59"] "18:00"
programmers.co.kr
- TC 18번 미통과
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
string solution(int n, int t, int m, vector<string> timetable) {
string answer = "";
//1. 시간대별 대기인원의 수 확인 필요없고
//2. 마지막 탑승차량에 탈건데 제일 늦게나올려면 몇시에 나와야하나
//3. 막차 이전까지의 탑승객 제외
//4. 막차 이후 탑승객 제외
//5. 진짜 막차 기다리는 사람들만 비교
sort(timetable.begin(), timetable.end());
//stirng 시간 int 분단위로 변경 vector 저장
vector<int>people;
for (int i = 0; i < timetable.size(); i++) {
timetable[i].erase(2, 1);
int hour = stoi(timetable[i].substr(0, 2)) * 60;
int minute = stoi(timetable[i].substr(2, 2));
int num = hour + minute;
people.push_back(num);
}
// 막차전에 보낼사람 먼저 보내기
for (int i = 0; i < n - 1; i++) {
int time = 9 * 60;
time = time + (t * i);
int cnt = 0;
while (1) {
if (cnt == m) break;
if (people[0] <= time) {
people.erase(people.begin());
cnt++;
}
else break;
}
}
// 막차시간
int lastTime = (9 * 60) + (t * (n - 1));
// 막차후에 사람들 버리기
for (int i = 0; i < people.size(); i++) {
if (people[i] > lastTime)
people.erase(people.begin() + i);
}
// 막차대기인원
int lastPeople = people.size();
int result;
// 막차대기인원이 수용가능인원보다 적다면 막차시간에 나오면 됨
if (lastPeople < m)result = lastTime;
// 막차대기인원이 수용가능인원보다 많다면 수용가능인원의 마지막 대기순서보다 1분 빨리나오기
else result = people[m-1] - 1;
int hour = result / 60;
int minute = result % 60;
if (hour < 10) answer += "0";
answer += to_string(hour);
answer += ':';
if (minute < 10)answer += "0";
answer += to_string(minute);
return answer;
}
int main() {
//cout << solution(1, 1, 5, { "08:00", "08:01", "08:02", "08:03" }) << endl;// "09:00"
//cout << solution(2, 10, 2, { "09:10", "09:09", "08:00" }) << endl;// "09:09"
//cout << solution(2, 1, 2, { "09:00", "09:00", "09:00", "09:00" }) << endl;// "08:59"
//cout << solution(1, 1, 5, { "00:01", "00:01", "00:01", "00:01", "00:01" }) << endl;// "00:00"
//cout << solution(1, 1, 1, { "23:59" }) << endl;// "09:00"
//cout << solution(10, 60, 45, { "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59" });// "18:00"
//cout << solution(2, 10, 3, { "09:05", "09:09", "09:13" });// "09:10"
cout << solution(1, 1, 5, { "00:01", "00:01", "00:01", "00:01", "00:01", "00:02", "00:03", "00:04" }); /// "00:00"
return 0;
}
'APS > 프로그래머스' 카테고리의 다른 글
[해시]위장 (0) | 2021.12.18 |
---|---|
[Summer/Winter Coding(2019)] 멀쩡한 사각형 C++ (0) | 2021.12.18 |
[그래프] 순위 C++ (0) | 2021.12.17 |
[2018 KAKAO BLIND RECRUITMENT] [1차] 뉴스 클러스터링 C++ (0) | 2021.12.17 |
[2017 카카오코드 본선]리틀 프렌즈 사천성 C++ (0) | 2021.12.09 |