쉬웠다. queue를 이용해서 그냥 구현해주면 되었다.
쉽게 시간을 1씩 늘려가면서 구현하면 되었다.
#include <string>
#include <vector>
#include <queue>
using namespace std;
int solution(int bridge_length, int weight, vector<int> truck_weights) {
int answer = 0;
int ind = 0;
int sum = 0;
queue<int> bridge;
while (1)
{
if (ind == truck_weights.size())
return answer + bridge_length;
answer++;
if (bridge.size() == bridge_length)
{
sum -= bridge.front();
bridge.pop();
}
if (sum + truck_weights[ind] <= weight)
{
sum += truck_weights[ind];
bridge.push(truck_weights[ind]);
ind++;
}
else
bridge.push(0);
}
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 카펫 (0) | 2022.05.28 |
|---|---|
| [프로그래머스] (C++) LV2 H-Index (0) | 2022.05.28 |
| [프로그래머스] (C++) LV2 위장 (0) | 2022.05.26 |
| [프로그래머스] (C++) LV2 배달 (0) | 2022.05.25 |
| [프로그래머스] (C++) LV2 괄호 회전하기 (0) | 2022.05.25 |