signal: segmentation fault (core dumped)
프로그래머스의 '다리를 지나는 트럭' 이라는 문제입니다
#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> que;
vector<int> counter;
int b_on_weight = 0;
int time = 1;
for (int i = 0; i < truck_weights.size(); i++)
{
if (que.size() < bridge_length)
{
if (b_on_weight + truck_weights[i] > weight)
{
que.push(truck_weights[i]); //트럭을 집어넣는다
b_on_weight += truck_weights[i]; //다리위의 총 트럭무게
counter.push_back(time); //i번째트럭을 언제넣었는지 기록
}
else
i--;
}
else
{
i--;
} //다리가 무거우면...시간은 흘러가지만 대기트럭은 계속 대기
//시간은 흐르고 트럭은 다리를 건넌다
for (int j = 0; j < truck_weights.size(); j++)
{
if (counter[j] + bridge_length == time)
que.pop();
}
time++;
}
answer = time;
return answer;
}