FastPrepCount Paths from the Top Left to the Bottom Right
Problem · Dynamic Programming

Count Paths from the Top Left to the Bottom Right

Learn this problem
MediumAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

A robot starts in the top-left cell of a grid with rows rows and columns columns. It must reach the bottom-right cell.

From each cell, the robot may move exactly one cell right or exactly one cell down. Return the total number of distinct valid paths.

Function

countGridPaths(rows: int, columns: int) → long

Examples

Example 1

rows = 3columns = 4return = 10

Every path contains two down moves and three right moves; there are 10 orderings.

Example 2

rows = 1columns = 7return = 1

Only repeated right moves are possible.

Example 3

rows = 5columns = 5return = 70

There are 70 ways to arrange four down and four right moves.

Constraints

  • 1 <= rows, columns <= 30.
  • The answer fits in a signed 64-bit integer.

More Amazon problems

drafts saved locally
public long countGridPaths(int rows, int columns) {
  // write your code here
}
rows3
columns4
expected10
checking account