Skip to content

Commit

Permalink
이슈 #406에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 2, 2024
1 parent 79cc23f commit 3e653b9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Programmers/다리를_지나는_트럭.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <string>
#include <vector>
#include <queue>

int solution(int bridge_length, int weight, std::vector<int> truck_weights) {
int time = 0, current_weight = 0;
std::queue<int> bridge;
std::queue<int> entry_time;

for (int i = 0; i < bridge_length; i++) {
bridge.push(0);
}

int truck_idx = 0;
while (truck_idx < truck_weights.size() || current_weight > 0) {
time++;

current_weight -= bridge.front();
bridge.pop();

if (truck_idx < truck_weights.size() &&
current_weight + truck_weights[truck_idx] <= weight) {
bridge.push(truck_weights[truck_idx]);
current_weight += truck_weights[truck_idx];
truck_idx++;
} else {
bridge.push(0);
}
}

return time;
}

0 comments on commit 3e653b9

Please sign in to comment.