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

Date:     Updated:

카테고리:

태그:

난이도

⭐⭐

📜문제

image image

🔎접근

도로를 하나의 Queue로 생각하고 도로의 길이를 Size로 생각해서 접근했다.

🌞풀이

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

using namespace std;

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

    int truck_num = 0;
    int total = 0;
    while (1)
    {
        //도로가 가득 찬 경우
        if (q.size() == bridge_length)
        {
            total -= q.front();
            q.pop();
        }
        // 트럭이 다 지나간 경우
        if (truck_num >= truck_weights.size())
        {
            answer += bridge_length;
            break;
        }
        // 도로가 가득 찬 경우 && 도로가 버틸 수 있는 무게를 넘어간 경우
        if (q.size() < bridge_length && truck_weights[truck_num] + total <= weight)
        {
            q.push(truck_weights[truck_num]);
            total += truck_weights[truck_num];
            truck_num++;
        }
        else q.push(0);

        answer++;
    }
    return answer;
}

❗주의

문제를 명확하게 푼 것이 아니라서 왜 맞는지 잘 모르겠다.

맨 위로 이동하기

Programmers 카테고리 내 다른 글 보러가기

댓글남기기