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

Seulguo·2022년 7월 6일
0

Algorithm

목록 보기
13/185
post-thumbnail
post-custom-banner

🐣 문제

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42583


🐥 코드

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

using namespace std;

int solution(int bridge_length, int weight, vector<int> truck_weights) {
    int answer = 0;
    int index = 0;
    int sum = 0;
    queue<int> q; 
    
    while(1){
        
        if(index == truck_weights.size()){
            answer += bridge_length;
            break;
        }
        
        answer ++;
        
        int tmp = truck_weights[index];
        
        if(q.size() == bridge_length){
            sum -= q.front();
            q.pop();
        }
        
        if(sum + tmp <= weight){
            q.push(tmp);
            sum += tmp;
            index ++;
        }
        else{
            q.push(0);
        }
    }
    
    return answer;
}
post-custom-banner

0개의 댓글