728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42583
소스코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public int solution(int bridge_length, int weight, int[] truck_weights)
{
int answer = 0;
Queue<int> bridgeQ = new Queue<int>();
int currentWeight = 0;
int finishTruck = 0;
while (true)
{
if (finishTruck == truck_weights.Length)
break;
if (bridgeQ.Count == bridge_length)
currentWeight -= bridgeQ.Dequeue();
int nextWeight = currentWeight + truck_weights[finishTruck];
if (nextWeight <= weight)
{
currentWeight += truck_weights[finishTruck];
bridgeQ.Enqueue(truck_weights[finishTruck]);
finishTruck++;
}
else
{
bridgeQ.Enqueue(0);
}
answer++;
}
return answer + bridge_length;
}
}
728x90
'코딩공부 > 프로그래머스' 카테고리의 다른 글
[프로그래머스]Lv.2 게임 맵 최단거리 C#(BFS) (0) | 2023.08.02 |
---|---|
[프로그래머스]Lv.2 영어 끝말잇기 C# (0) | 2023.08.02 |
[프로그래머스]Lv.2 주식가격C# (0) | 2023.08.01 |
[프로그래머스]Lv.2 기능개발C# (0) | 2023.08.01 |
※[프로그래머스]Lv.2 가장 큰 수C#(문자열 CompareTo 정렬, 시간복잡도 오류) (0) | 2023.08.01 |