File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments