Skip to content

Commit 2eb926a

Browse files
committed
Queue 다리를 지나는 트럭
1 parent 1f6efc3 commit 2eb926a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Queue/Queue1.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
class Solution {
5+
public int solution(int bridge_length, int weight, int[] truck_weights) {
6+
int time = 0;
7+
8+
int totalWeight = 0;
9+
int truckIndex = 0;
10+
Queue<Truck> truckQueue = new LinkedList<Truck>();
11+
12+
while (truckIndex < truck_weights.length) {
13+
time++;
14+
15+
if (!truckQueue.isEmpty()) {
16+
Truck truck = truckQueue.peek();
17+
18+
if (time - truck.getEnterTime() == bridge_length) {
19+
totalWeight -= truck.getWeight();
20+
truckQueue.poll();
21+
}
22+
}
23+
24+
int nextTruck = truck_weights[truckIndex];
25+
if (totalWeight + nextTruck <= weight) {
26+
truckQueue.offer(new Truck(nextTruck, time));
27+
totalWeight += nextTruck;
28+
truckIndex++;
29+
}
30+
}
31+
32+
time += bridge_length;
33+
34+
return time;
35+
}
36+
}
37+
38+
class Truck {
39+
private int weight;
40+
private int enterTime;
41+
42+
public Truck(int weight, int enterTime) {
43+
this.weight = weight;
44+
this.enterTime = enterTime;
45+
}
46+
47+
public int getWeight() {
48+
return weight;
49+
}
50+
51+
public int getEnterTime() {
52+
return enterTime;
53+
}
54+
}

0 commit comments

Comments
 (0)