프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

  • 콘이 최대한 늦게 버스 정류장에 도착하여 사무실로 가려면 마지막 버스를 마지막으로 타면 됨

 

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// 셔틀은 9시부터 t분 간격으로 n번 도착
// 셔틀이 도착한 순간에 자리가 남고 그 순간에 도착한 크루가 있으면 탑승 가능
// 콘은 최대한 늦게, 그러니까 마지막 버스를 타야함
// 일단 시:분 형태로 주어지는 문자열을 시 * 60 + 분 형태의 int형으로 변환

string solution(int n, int t, int m, vector<string> timetable) {
    string answer = "";
    int time = 0;
    
    vector<int> timeTable;
    
    for(auto& time : timetable)
        timeTable.push_back(stoi(time.substr(0, 2)) * 60 + stoi(time.substr(3, 2)));
    
    sort(timeTable.begin(), timeTable.end());
    
    int cnt = 0;
    int arrivalTime = 540;
    
    for(int i = 1; i <= n; i++) {
        int cntInBus = 0;
        
        while(cntInBus < m && cnt < timeTable.size()) {
            if(timeTable[cnt] <= arrivalTime) {
                cntInBus++;
                cnt++;
            }
            else 
                break;
        }
        
        // 마지막 버스일 때
        if(i == n) {
            // 자리가 남으면 버스 도착시간에 맞춰 오면 됨
            if(cntInBus < m)
                time = arrivalTime;
            // 자리가 안남으면 마지막 사람보다 1분 일찍 오면 됨
            else
                time = timeTable[cnt - 1] - 1;
        }
        
        arrivalTime += t;
    }
    
    int hour = time / 60;
    int minute = time % 60;
    
    if(hour < 10)
        answer = "0" + to_string(hour) + ":";
    else 
        answer = to_string(hour) + ":";
    
    if(minute < 10)
        answer += "0" + to_string(minute);
    else
        answer += to_string(minute);
    
    return answer;
}

 

+ Recent posts