Google questions

Grid Paths

GoogleOnsiteMedium

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.

Part 1: Count all paths

Return the number of paths from (n - 1, 0) to (n - 1, m - 1), modulo 1_000_000_007.

  1. Example 1
    m
    3
    n
    3
    Output
    2

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

Constraints

  • 1 <= n <= 1000
  • 1 <= m <= 1000
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question