FastPrepCount Rightward Grid Paths

Count Rightward Grid Paths

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

A board has rows rows and columns columns. Start at [startRow,0] and reach [endRow,columns-1].

Each move advances one column right and changes the row by -1, 0, or +1. The row must stay inside the board.

Return the number of distinct paths modulo 1,000,000,007.

Function

countRightwardPaths(rows: int, columns: int, startRow: int, endRow: int) → int

Examples

Example 1

rows = 3columns = 3startRow = 2endRow = 2return = 2

The row sequences are 2-1-2 and 2-2-2.

Example 2

rows = 1columns = 4startRow = 0endRow = 0return = 1

Only straight-right moves stay in the one-row board.

Example 3

rows = 2columns = 2startRow = 0endRow = 1return = 1

One down-right move reaches the target.

Constraints

  • 1 <= rows, columns <= 100000.
  • 0 <= startRow, endRow < rows.
  • rows * columns <= 2,000,000.

More Google problems

See Google hiring insights
public int countRightwardPaths(int rows, int columns, int startRow, int endRow) {
    // Write your code here.
}
rows3
columns3
startRow2
endRow2
expected2
Checking account…