Problem Statement
Each boulder has some number of sides, and as it travels down the mountain, it rolls through all of its sides, completing a cycle and repeating it until it reaches the bottom of the mountain. The boulder slides down the mountain for one time unit on each side. For instance, if a boulder has three sides A, B and C, it will travel down the mountain starting on side A. After sliding down for one time unit, it will roll to side B, and after one more time unit, it will roll to side C. After one more time unit, it will return to side A and repeat the cycle until it reaches the bottom. Each side has a number between 1 and 9, inclusive, indicating the number of feet the boulder will travel on that side in one time unit. All progress is measured only at the end of each time unit (see examples for further clarification).
You will be given a
Definition
- Class:
- BoulderRace
- Method:
- winner
- Parameters:
- String[], int
- Returns:
- int
- Method signature:
- int winner(String[] boulders, int distance)
- (be sure your method is public)
Constraints
- boulders will contain between 2 and 5 elements, inclusive.
- Each element of boulders will contain between 1 and 5 characters, inclusive.
- Each character in boulders will be a digit between '1' and '9', inclusive.
- distance will be between 1 and 1000, inclusive.
Examples
{"9191", "9911"}
38
Returns: 1
The boulders are placed 38 feet above the base of the mountain and roll down according to the table below: 1| 2| 3| 4| 5| 6 Boulder1| 9|10|19|20|29|30 Boulder2| 9|18|19|20|29|38 After 6 time units the second boulder reaches the bottom of the mountain and is crowned the winner.
{"23", "1254", "81111", "3141"}
24
Returns: 1
1| 2| 3| 4| 5| 6| 7| 8 Boulder1| 2| 5| 7|10|12|15|17|20 Boulder2| 1| 3| 8|12|13|15|20|24 Boulder3| 8| 9|10|11|12|20|21|22 Boulder4| 3| 4| 8| 9|12|13|17|18
{"1234", "1144", "4411", "2323", "4321"}
100
Returns: 0
Note that all the boulders will reach the bottom at the same time. If a set of boulders tie, choose the boulder with the lower index from among the set.
{"1", "112", "1113"}
12
Returns: 2
{"1234", "2341", "3412", "4123", "4321"}
24
Returns: 3
{"151", "331"}
17
Returns: 1
{"56", "78", "88", "94", "9"}
100
Returns: 4
{"9", "8", "7", "6"}
71
Returns: 0
{"555", "6"}
30
Returns: 1
{"1", "5", "8"}
3
Returns: 1
{"1", "2", "5", "8", "9"}
100
Returns: 4
{"1", "1", "1"}
1
Returns: 0
{"1123", "1232", "3122", "4111", "3211"}
1000
Returns: 1
{"1123", "4111", "3122", "1232", "3211"}
1000
Returns: 2
{"1123", "4111", "3211", "3122", "1232"}
1000
Returns: 3
{"3122", "1232", "1123", "4111", "3211"}
1000
Returns: 0
{"1", "11", "111", "1111", "11111"}
1000
Returns: 0
{"71", "44", "17"}
103
Returns: 0
{"17", "44", "71"}
103
Returns: 2
{"671", "842"}
993
Returns: 0
{"842", "671"}
993
Returns: 1
{"11111", "9"}
26
Returns: 1
{"9", "88", "777", "6666", "55555"}
22
Returns: 0
{"88", "99", "77"}
12
Returns: 0
Be careful! All boulders reach the bottom in the 2nd time unit. At first glance one might think the second boulder is the winner; however, since all three boulders reached the bottom in the same time unit, the first boulder wins. 1. All progress is measured at the end of a time unit. 2. In case of a tie, return the lower index.
{"5555", "4" }
4
Returns: 0
{"9", "1" }
1
Returns: 0
{"1", "2" }
1000
Returns: 1
{"11", "22" }
3
Returns: 1
{"1", "11111" }
1000
Returns: 0
{"33333", "17" }
16
Returns: 1