-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue1.java
More file actions
54 lines (42 loc) · 1.26 KB
/
Copy pathQueue1.java
File metadata and controls
54 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.LinkedList;
import java.util.Queue;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int time = 0;
int totalWeight = 0;
int truckIndex = 0;
Queue<Truck> truckQueue = new LinkedList<Truck>();
while (truckIndex < truck_weights.length) {
time++;
if (!truckQueue.isEmpty()) {
Truck truck = truckQueue.peek();
if (time - truck.getEnterTime() == bridge_length) {
totalWeight -= truck.getWeight();
truckQueue.poll();
}
}
int nextTruck = truck_weights[truckIndex];
if (totalWeight + nextTruck <= weight) {
truckQueue.offer(new Truck(nextTruck, time));
totalWeight += nextTruck;
truckIndex++;
}
}
time += bridge_length;
return time;
}
}
class Truck {
private int weight;
private int enterTime;
public Truck(int weight, int enterTime) {
this.weight = weight;
this.enterTime = enterTime;
}
public int getWeight() {
return weight;
}
public int getEnterTime() {
return enterTime;
}
}