우당탕탕 개발일지

[프로그래머스] Level 3. 셔틀버스 본문

Algorithm(c++)/Level 3

[프로그래머스] Level 3. 셔틀버스

devchop 2025. 3. 12. 15:16

https://school.programmers.co.kr/learn/courses/30/lessons/17678

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

해결방법

  1. 먼저 대기하는 사람순으로 정렬한다.
  2. 셔틀버스를 앞에서부터 하나씩 크루를 태운다. 
  3. 마지막 셔틀버스일때, 수용인원이 가득 찼다면, 맨마지막으로 탄 크루보다 1분 일찍 탄다.
  4. 만약 수용인원이 가득차지 않았다면, 셔틀버스가 도착한 시간에 탄다.

 

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

using namespace std;

string solution(int n, int t, int m, vector<string> timetable) {
    string answer = "";
    
    vector<int> minutes;
    for(int i=0; i<timetable.size(); i++){
        int m = stoi(timetable[i].substr(0,2))*60 + stoi(timetable[i].substr(3,2));
        minutes.push_back(m);
    }
    
    sort(minutes.begin(), minutes.end());

    int ts = 9*60; //9시에 도착함.
    int index = 0;
    
    while(n>0){
        //멤버수만큼
        int i=0;
        for(;i<m; i++){
            if(index < minutes.size() && minutes[index]<=ts) index++;
            else break;
        }   
        
        if(n>1){
            n--;
            ts+=t; //도착시간증가    
            continue;
        }
        
        int my = 0;
        if(i == m) my = minutes[index-1]-1; //인원꽉찼음.
        else my = ts; //버스출발시간에 맞춰서옴.    
        
        string hourStr = my/60 <10? "0"+to_string(my/60) :to_string(my/60);
        string minuteStr = my%60 <10 ? "0"+to_string(my%60) : to_string(my%60);
        return hourStr+":"+minuteStr;
    }
    
    
    return 0;
}