Grid Paths

Asked at GoogleOnsite

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

  1. Example 1

    Input: n = 3, m = 3

    Output: 2

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.