Stepping stone crossing
HardOnline Assessment
Plan a frog's crossing of a river of stepping stones, and find which stones it cannot avoid.
river is a string with one character per stone, from the start bank to the far bank: "." is a safe stone and "x" is a broken stone that can never be landed on. The frog starts on stone 0 and wants to reach the last stone.
The frog may leap by any length listed in leaps, either forward or backward (as long as it stays on the river), and must land on a safe stone each time. A leap length may be listed more than once, but that does not create different routes. A route is the sequence of stones the frog lands on, so two routes are different if these sequences differ.
Input: river and leaps.
Output: a list of three integers [fewest, routes, forced]:
fewest: the smallest number of leaps needed to reach the last stone.routes: how many different routes use exactlyfewestleaps, modulo1,000,000,007.forced: how many stones are landed on by every one of those routes, not counting stone0and the last stone themselves.
If the last stone cannot be reached (including a broken first or last stone, or an empty river), return [-1, 0, 0]. If the river has a single safe stone, the frog is already across: return [0, 1, 0].
The shortest routes can number in the billions, so listing them is out of the question.
Example 1
- Input:
- ["........",[3,5]]
- Output:
- [3,1,2]
- Explanation
- A leap of 5 goes forward, and a leap of 3 can go backward. The only 3-leap route is 0 -> 5 -> 2 -> 7: forward 5, back 3, forward 5. There is no shorter route and no other 3-leap route, and the stones 5 and 2 are used by that single route, so the answer is [3, 1, 2].
Example 2
- Input:
- ["......x..x..",[1,4]]
- Output:
- [4,2,2]
- Explanation
- Stones 6 and 9 are broken. The shortest crossings take 4 leaps: 0 -> 4 -> 8 -> 7 -> 11 and 0 -> 4 -> 3 -> 7 -> 11 (one backward leap of 1 in each). Both routes land on stones 4 and 7, and only those two are shared, so the answer is [4, 2, 2].
Example 3
- Input:
- ["......",[2,4]]
- Output:
- [-1,0,0]
- Explanation
- Every leap is even, so the frog can only ever reach even-numbered stones and can never land on stone 5. The answer is [-1, 0, 0].
Constraints
0 <= river.length <= 12,000- every character of
riveris"."or"x" 1 <= leaps.length <= 40, and1 <= leaps[i] <= 12,000; values may repeat