Coding_Test 연습/Programmers

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

Codetesing 2022. 5. 26. 21:04

쉬웠다. queue를 이용해서 그냥 구현해주면 되었다.

쉽게 시간을 1씩 늘려가면서 구현하면 되었다.

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

using namespace std;

int solution(int bridge_length, int weight, vector<int> truck_weights) {
    int answer = 0;
    int ind = 0;
    int sum = 0;
    queue<int> bridge;

    while (1)
    {
        if (ind == truck_weights.size())
            return answer + bridge_length;

        answer++;
        
        if (bridge.size() == bridge_length)
        {
            sum -= bridge.front();
            bridge.pop();
        }

        if (sum + truck_weights[ind] <= weight)
        {
            sum += truck_weights[ind];
            bridge.push(truck_weights[ind]);
            ind++;
        }
        else
            bridge.push(0);
    }
}