-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurvo.java
More file actions
129 lines (85 loc) · 2.19 KB
/
Turvo.java
File metadata and controls
129 lines (85 loc) · 2.19 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
1,0,2,0,0,-2,-3,0
1,2,0,0,0,-2,-3,0
1,2,-2,0,0,0,-3,0
1,2,-2,-3,0,0,0,0
1,2,-2,-3,0,0,0,0
Public void rearrageArray(int [] arr) {
Int lastSeenZero = -1; //1,2,3
for(int i = 0 ; i < arr.length ; i++) {
if(arr[i] != 0 && lastSeenZero != -1) {
swap(arr,i,lastSeenZero));
lastSeenZero = lastSeenZero+1;
} else if(arr[i] == 0 && lastSeenZero == -1) lastSeenZero = i;
}
for(int i : arr) sysout(i);
}
Public void swap(int[] arr, int start, int end) {
Int temp = arr[start];
Arr[start] = arr[end];
Arr[end] = temp;
}
ABC Trucking
Hyd -> Blore
Food
Petrol
Fireworks
….
Petrol-Fireworks
Food-Fireworks
2 Trucks are suff. To transport all goods?
Yes
Food
Petrol
Fireworks
….
Petrol-Fireworks
Food-Fireworks
Petrol-Food
2 Trucks are suff. To transport all goods?
No
Public String doesTrucksSufficient(List<String> items, Map<String,List<String>> incompatabilies) {
Set<String> truckOneItems = new HashSet<>();
Set<String> truckTwoItems = new HashSet<>():
//if(truckOneItems.contains(items.get(i)) || truckTwoItems.contains(items.get(i)) continue;
for(int i = 0 ; i < items.size() ; i++) {
List<String> notCompatable = map.get(items.get(i));
if(truckOneItems.size() == 0) {
truckOneItems.add(items.get(i));
String val = checkCompatablity(truckTwoItems,notCompatable);
if(val.equals(“No”)) return val;
}
Else {
if(truckOneItems.split(incompatabilies.get(items(i)).size() == 0) {
truckOneItems.add(items(i));
if(truckTwoItems.split(notCompatable).size() == 0) {
String val = checkCompatablity(truckTwoItems,notCompatable);
if(val.equals(“No”)) return val;
} else {
Return “No”;
}
}
Else if(truckTwoItems.split(incompatabilities.get(items(i)).size() == 0) {
truckTwoItems.add(items(i));
if(truckOneItems.split(notCompatable).size() == 0) {
String val = checkCompatablity(truckOneItems,notCompatable);
if(val.equals(“No”)) return val;
} else {
Return “No”;
}
}
}
}
Return “Yes”;
}
Public void checkCompatibility(List<String> truckTwoItems,List<String> notCompatable){
for(int j = 0 ; j < notCompatable.size(); j++){
if(truckTwoItems.split(notCompatable.get(j)) == 0){
truckTwoItems.add(notCompatable);
}else {
Return “No”;
}
}
}
1 - 2, 3
2 - 1, 3
3 - 1, 2