[프로그래머스/C++] 다리를 지나는 트럭

mani·2023년 4월 16일
0

다리를 지나는 트럭

두 개 정도만 멤버 변수로 필요한 경운 pair 사용

#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(int bridge_length, int weight, vector<int> truck_weights) {
    int answer = 0;
    
    int time = 0;
    int now_weight = 0;
    int cnt = 0;
    queue<pair<int, int>> move_truck;
    
    while(1){
        if(weight >= now_weight + truck_weights.at(cnt)){
            move_truck.push(make_pair(truck_weights[cnt], bridge_length + 1 + time));
            now_weight += truck_weights[cnt];
            cnt++;                            
        }
        
        if(cnt >= truck_weights.size()){
            answer = move_truck.back().second;
            break;
        }
        else{
            time++;
            if(move_truck.front().second == time + 1){
                now_weight -= move_truck.front().first;
                move_truck.pop();
            }
        }
    }
    
    
    return answer;
}
profile
log

0개의 댓글