Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Last active May 20, 2020 00:38
Show Gist options
  • Save DongguemYoo/be210d58bed927e21a039fc87688fb07 to your computer and use it in GitHub Desktop.
Save DongguemYoo/be210d58bed927e21a039fc87688fb07 to your computer and use it in GitHub Desktop.
코딩테스트 연습 > 스택/큐 > 다리를 지나는 트럭.cs
//검색한것도 없고 딱히 어려운 부분도 없었다.
//바로바로 디버깅을 해서 인덱스를 조절하면서 했다...아직 머리속으로 바로는 잘 안되는듯..
//제한시간을 줄이기위해 로직을 일부 변경했다.(마지막 체크)
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights)
{
int second = 0;
Queue<int> bridge = new Queue<int>();
Queue<int> truks = new Queue<int>(truck_weights);
for (int i=0;i< bridge_length;i++)
bridge.Enqueue(0);
int CurrnetWGT = 0;
//트럭이 모두 빠져나갈때까지 반복
while (truks.Count != 0)
{
//시간증가
second++;
if (bridge.Peek() != 0)
{
//트럭이 빠져나갔다면
CurrnetWGT -= bridge.Peek();
//전체 중량에서 -한다.
bridge.Dequeue();
}
else
bridge.Dequeue();
//현재중량에서 다음트럭을 넣을수있다면 트럭을 집어넣고
//아니라면 0을 집어넣는다
if (CurrnetWGT + truks.Peek() <= weight)
{
CurrnetWGT += truks.Peek();
bridge.Enqueue(truks.Dequeue());
continue;
}else
bridge.Enqueue(0);
}
if (bridge.Sum() != 0)
second += bridge_length;
return second;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment