Grid Paths
Asked at Google
Reported by the problem's creator, not verified
You are given an n by m grid. Rows are numbered 0 to n - 1 from top to bottom and columns 0 to m - 1 from left to right.
A path starts at the bottom-left cell (n - 1, 0) and ends at the bottom-right cell (n - 1, m - 1). From a cell (r, c) one move goes to (r - 1, c + 1), (r, c + 1) or (r + 1, c + 1), and must stay inside the grid. Every move advances exactly one column, so a path holds exactly one cell in each column.
Implement count_paths(n, m), returning the number of valid paths modulo 1_000_000_007.
Examples
Example 1
Input: n =
3, m =3Output:
2Explanation: The path occupies one cell in each of the 3 columns. Ending in the bottom row after two up/right/down moves leaves exactly 2 paths.