Problem Statement
In Ukraine, the road intersections can be controlled or non-controlled. If an intersection is controlled by a traffic light, your actions are simple - go only if the green light is on. Things get more complicated when you get to a non-controlled intersection.
You are given a
When a car gets to the intersection, the driver must check whether any other cars are approaching the intersection from other roads. If he is the only driver at the intersection, he can go immediately. If there are at least 2 cars, they need to determine who will go first using the following rules:
- If one of the roads is major, then the car entering the intersection from that road gets the priority.
- If there is no major road or there are no cars on the major road, then the rule of the right hand is applied. Every driver at the intersection looks on the road which is the next to his right (road 1 is to the right of road 0, road 2 is to the right of road 1 and road 0 is to the right of road 2). If any driver doesn't have a car to his immediate right, he gets the priority.
- If there are cars on all roads, then the drivers themselves determine who will go first. We assume that the car marked by the alphabetically first letter will go first.
As soon as one of the cars gets the priority to go, it passes the intersection and allows the next car on the same road (if any) to get to the intersection. The process is repeated with the new car taken into account. Given an
Definition
- Class:
- DriveOrder
- Method:
- determineOrder
- Parameters:
- String[], int
- Returns:
- String
- Method signature:
- String determineOrder(String[] cars, int major)
- (be sure your method is public)
Constraints
- cars will contain exactly 3 elements.
- Each element of cars will contain only uppercase letters ('A' - 'Z').
- All characters in cars will be distinct.
- major will be between -1 and 2, inclusive.
Examples
{"A", "", ""}
1
Returns: "A"
Only one car.
{"", "AZBDE", ""}
2
Returns: "AZBDE"
All cars approach the intersection from the same road, so they pass it in the same order.
{"A", "B", ""}
1
Returns: "BA"
The car at the major road gets the priority and goes first.
{"A", "B", "CD"}
2
Returns: "CDBA"
Cars C and D go first because they are at the major road. Then car B goes because it doesn't have any cars to its right.
{"A", "B", "C"}
-1
Returns: "ACB"
No major road and all roads are occupied. Rule 3 is applied, and car A goes first. Car C follows because it doesn't have any cars to its right.
{"AD", "BC", "EF"}
-1
Returns: "ABCDEF"
{"","",""}
1
Returns: ""
No cars.
{"Z","GHIJKLMNOPQRSTUVWXABCDEF","Y"}
-1
Returns: "GHIJKLMNOPQRSTUVWXABCDEFZY"
Rule 3 used a lot.
{"","A","ZYXWVUQPONMLKJIHGFEDCBRST"}
-1
Returns: "ZYXWVUQPONMLKJIHGFEDCBRSTA"
Rule 2 used a lot.
{"GHJAKSLDY","PQOWIEZUTR","MNBFXCV"}
-1
Returns: "GHJAKMNBFPQOSLDWIEXCVZUTRY"
Chaotic rule 3 merging.
{ "XVARLJWUMFKQITH", "EGN", "DPOSBC"}
0
Returns: "XVARLJWUMFKQITHDPOSBCEGN"
{ "TALDPEFVIKMSBGOC", "HRUQ", "JN"}
2
Returns: "JNHRUQTALDPEFVIKMSBGOC"
{ "X", "LUMHIJWVQDCGPATOERNKBFYS", ""}
0
Returns: "XLUMHIJWVQDCGPATOERNKBFYS"
{ "AVLCX", "KPUIEODRTHSFBYGMNJ", "ZWQ"}
-1
Returns: "AKPUIEODRTHSFBVLCXZWQYGMNJ"
{ "LNOECXUTGPJVIQKHD", "MRBS", "WAF"}
0
Returns: "LNOECXUTGPJVIQKHDWAFMRBS"
{ "PRWAFCDTBLEVOHGN", "UKMSIQJ", ""}
2
Returns: "UKMSIQJPRWAFCDTBLEVOHGN"
{ "VFQSTNARDOLB", "G", "UEKHJIMPWCX"}
-1
Returns: "GVFQSTNARDOLBUEKHJIMPWCX"
{ "", "KDFBHOLMRQSICPJA", "EGN"}
1
Returns: "KDFBHOLMRQSICPJAEGN"
{ "MNHOKAGEPFCWRUTYBDXIJVZ", "SL", "Q"}
2
Returns: "QSLMNHOKAGEPFCWRUTYBDXIJVZ"
{ "L", "BPNGWKQMRVXHEYCSUDT", "OJFIA"}
0
Returns: "LOJFIABPNGWKQMRVXHEYCSUDT"
{ "ARWCKXTIP", "DSBQNHG", "LEJOMVUF"}
1
Returns: "DSBQNHGARWCKXTIPLEJOMVUF"
{ "HKY", "DALTINXSJMQFCWGO", "ERVUBP"}
0
Returns: "HKYERVUBPDALTINXSJMQFCWGO"
{ "QZYSKGTLBUX", "HPEWVJICNDFOAMR", ""}
0
Returns: "QZYSKGTLBUXHPEWVJICNDFOAMR"
{ "", "YFRVCKQWHTLOGIESJMABZD", "PXNU"}
2
Returns: "PXNUYFRVCKQWHTLOGIESJMABZD"
{ "H", "JIEMKB", "LNOCAFGD"}
1
Returns: "JIEMKBHLNOCAFGD"
{ "AGVWRDLKUSTIQFBHYEZXPJO", "N", "MC"}
2
Returns: "MCNAGVWRDLKUSTIQFBHYEZXPJO"
{ "QFVSJXERWKNYLHUATMDBGZICPO", "", ""}
-1
Returns: "QFVSJXERWKNYLHUATMDBGZICPO"
{ "XDGFBEMNCWHKQO", "JPIVR", "LSUAT"}
0
Returns: "XDGFBEMNCWHKQOLSUATJPIVR"
{ "A", "WQJGCRKMUDLSPFEIOB", "HNVT"}
1
Returns: "WQJGCRKMUDLSPFEIOBAHNVT"
{ "CHYUDNMOFAZVKEQWIBPJXS", "TGRL", ""}
0
Returns: "CHYUDNMOFAZVKEQWIBPJXSTGRL"
{"", "", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
1
Returns: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
A traffic jam.
{"", "", ""}
2
Returns: ""
An empty crossing.