Count Rightward Grid Paths
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) → intExamples
Example 1
rows = 3columns = 3startRow = 2endRow = 2return = 2The row sequences are 2-1-2 and 2-2-2.
Example 2
rows = 1columns = 4startRow = 0endRow = 0return = 1Only straight-right moves stay in the one-row board.
Example 3
rows = 2columns = 2startRow = 0endRow = 1return = 1One down-right move reaches the target.
Constraints
1 <= rows, columns <= 100000.0 <= startRow, endRow < rows.rows * columns <= 2,000,000.