제일 큰 문제는 글을 이해하지 못하는 내 문해력...!
계속 반복해서 읽어도 이해하지 못해서 직접 손으로 로직을 그리면서 이해했다.
질문하기 탭에서 다른 사람이 정리해준 글도 도움이 되었다.
문제를 겨우 이해했지만 로직을 짜는 건 힘들었다.
Queue를 사용해야 한다는 사실만 어렴풋이 짐작한채 나름 로직을 작성했다.
처음에 queue를 순회하고 무게 조건에 따라 제거 될때마다
다리 길이 - 현재 queue 길이 + 1 을 더해줬다.
이런 로직이면 트럭 다리를 지날때 맨 앞의 트럭만 이동하고 뒤의 트럭은 전혀 이동하지 못한 상태로 머무르게 된다.
using System;
using System.Collections.Generic;
public class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights)
{
int answer = 0;
Queue<int> queue = new Queue<int>();
int index = 0;
int currentW = 0;
int time = 0;
while (true)
{
if (index == truck_weights.Length) break;
if (queue.Count == bridge_length) currentW -= queue.Dequeue();
if (currentW + truck_weights[index] <= weight)
{
currentW += truck_weights[index];
queue.Enqueue(truck_weights[index]);
index++;
}
else
queue.Enqueue(0);
time++;
}
answer = time + bridge_length;
return answer;
}
}