-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_04_unsolved.java
More file actions
86 lines (73 loc) · 3.12 KB
/
Problem_04_unsolved.java
File metadata and controls
86 lines (73 loc) · 3.12 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
public class Problem_04_unsolved {
public static int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0;
int initialLength = truck_weights.length;
ArrayList<Integer> truckQueue = new ArrayList<>();
ArrayList<Integer> completedTruck = new ArrayList<>();
ArrayList<Integer> crossingTruck = new ArrayList<>();
HashMap<Integer, Integer> innerCounter = new HashMap<>(); // For counting per truck
for(int truck : truck_weights){
truckQueue.add(truck);
}
while(true) {
// exit condition
if (completedTruck.size() == initialLength) {
break;
}
// crossing truck -> completed truck
if(!crossingTruck.isEmpty()) {
for (Map.Entry<Integer, Integer> count : innerCounter.entrySet()) { // *
Integer truckWeight = count.getKey();
Integer proceeding = count.getValue();
if (proceeding == bridge_length) {
completedTruck.add(truckWeight);
innerCounter.remove(truckWeight);
crossingTruck.remove(truckWeight);
}
}
}
// truckQueue queue -> crossing truck
if(!truckQueue.isEmpty()){
if(crossingTruck.size() < bridge_length){
int sum = innerCounter.keySet().stream().mapToInt(Integer::intValue).sum() + truckQueue.get(0);
if(weight - sum>=0){
innerCounter.put(truckQueue.get(0), 1);
crossingTruck.add(truckQueue.remove(0));
} else {
for (Map.Entry<Integer, Integer> count : innerCounter.entrySet()) {
Integer truckWeight = count.getKey();
Integer proceeding = count.getValue();
innerCounter.put(truckWeight, proceeding+1);
}
}
} else {
continue;
}
} else {
break;
}
answer += 1;
}
return answer;
}
public static void main(String[] args) {
int bridge_length_1 = 2;
int bridge_length_2 = 100;
int bridge_length_3 = 100;
int weight_1 = 10;
int weight_2 = 100;
int weight_3 = 100;
int[] truck_weights_1 = {7, 4, 5, 6};
int[] truck_weights_2 = {10};
int[] truck_weights_3 = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
int result_1 = solution(bridge_length_1, weight_1, truck_weights_1);
// int result_2 = solution(bridge_length_2, weight_2, truck_weights_2);
// int result_3 = solution(bridge_length_3, weight_3, truck_weights_3);
System.out.println("result_1 = " + result_1); // 8
// System.out.println("result_2 = " + result_2); // 101
// System.out.println("result_3 = " + result_3); // 110
}
}