PROBLEM STATEMENT
We've all played those silly "Whack-A-Mole" games at those kids' places. Now,
the owner of Charles E. Swiss (a local "fun zone") has hired you to write the
software behind them. However, these games are not random, and each mole pops
up at a specified interval.
Create a class MoleWhack which contains a method howMany, which determines how
many moles will be whacked by the player, given the intervals and the spots
which the player whacks at each second. When a mole pops up, it stays up for
precisely one second, and must be whacked in exactly that second in order to
count as a hit.
DEFINITION
Class name: MoleWhack
Method name: howMany
Parameters: int[], int[]
Returns: int
The method signature is (make sure it is declared public): int howMany(int[]
intervals, int[] whacked);
Each element of intervals represents the interval for a given mole. That is, if
element 0 of intervals is 3, mole 0 pops up every 3 seconds, starting at second
3. If element 3 of intervals is 1, then mole 3 pops up every second.
whacked represents the location which the player whacks each second. That is,
element 0 corresponds to the location whacked at second 1, element 1
corresponds to the location whacked at second 2, etc.
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
* intervals and whacked will have between 1 and 50 elements, inclusive
* each element of intervals will be between 1 and 100, inclusive
* each element of whacked will be between 0 and n-1, inclusive, where n is the
number of moles (that is, the length of intervals)
EXAMPLES
Example 1:
intervals = {2,3,5}
whacked = {0,1,2,0,2,1}
So, mole 0 will pop up at seconds 2, 4, 6, 8, etc.,
mole 1 will pop up at seconds 3, 6, 9, 12, etc.,
and mole 2 will pop up at seconds 5, 10, 15, 20, etc.
At second 1, the player whacks location 0, but the mole there has not popped up.
At second 2, the player whacks location 1, but the mole there has not popped up.
At second 3, the player whacks location 2, but the mole there has not popped up.
At second 4, the player whacks location 0, and the mole there has popped up, so
add 1 hit.
At second 5, the player whacks location 2, and the mole there has popped up, so
add 1 hit.
At second 6, the player whacks location 1, and the mole there has popped up, so
add 1 hit.
Total of 3 hits, so return 3.
Example 2:
intervals = {1,2,4}
whacked = {0,1,0,2,0,2,1}
For each whack:
Location Hit
0 yes
1 yes
0 yes
2 yes
0 yes
2 no
1 no
Method returns 5
Example 3:
intervals = {9,3}
whacked = {0,0,1,1,0,1,0,0,1,0,1,0,1,1,1,0,1,1,0,1,0,0,0,0,1,1,0,1,0}
Method returns 6
Example 4:
intervals = {5,5,7,9,2,7,9}
whacked = {4,4,1,2,6,0,6,6,6,4,2,4,5,2,4,4,5,1,2,1,2,1,6,4,3,3,5,3,2,2,6,1}
Method returns 9
Example 5:
intervals = {1,3,1,9,2}
whacked =
{0,3,0,2,0,1,0,4,3,1,3,1,1,0,4,1,0,4,2,2,0,1,3,1,1,4,0,3,3,0,1,1,0,3,3,4,1,0}
Method returns 22