문제 링크

요약

  • 귀찮은 구현문제.

최종

  • 그냥 queue 하나 만들고 시간 증가시키면서 적절히 넣고 빼고 하면 된다.
    • 근데 문제 설명이 좀 그지같게 돼있는게, 크기 1인 트럭이 한번에 1씩 움직였을 때 bridge_length 만큼 이동하면 다리를 완전히 빠져나간다는 설명이 명시적으로 되어있지 않아 유추해야 된다.
    • 일부 프로그래머스의 문제 질이 좀 낮은듯하다.
#include <string>
#include <vector>
#include <queue>
 
using namespace std;
 
struct TruckInfo {
	int idx;
	int enter_time;
};
 
int solution(int bridge_length, int weight, vector<int> truck_weights) {
	int time = 0;
	int next = 0;
	queue<TruckInfo> q;
 
	// First truck
	time++;
	q.push({next, time});
	weight -= truck_weights[next];
	next++;
 
	while (!q.empty()) {
		auto &front = q.front();
 
		time++;
 
		if (time - front.enter_time == bridge_length) {
			q.pop();
			weight += truck_weights[front.idx];
		}
 
		if (next < truck_weights.size() && truck_weights[next] <= weight) {
			q.push({next, time});
			weight -= truck_weights[next];
			next++;
		}
	}
 
	return time;
}