Medium

Quiz

#576 Out of Boundary Paths

APPROACH

A ball starts at [startRow, startColumn] on an m x n grid. Each move shifts the ball to one of the four adjacent cells; moves that cross the boundary count as escaping. With at most maxMove total moves, count the number of distinct paths that take the ball out of bounds. Return the answer modulo 109 + 7.

Example 1:

Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6

Example 2:

Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12
1 of 4
1:00

What is the optimal approach for this problem?