Skip to content
Code it · Write it

Count the paths through a grid

Write unique_paths(rows, cols) (Python) or uniquePaths(rows, cols) (JavaScript/TypeScript). A robot starts in the top-left cell of a grid with rows rows and cols columns and may only step right or down. Return how many different paths take it to the bottom-right cell.

The starter makes the table for you: dp = table(rows, cols) is a list of lists (an array of arrays in JavaScript) that the picture shows, every cell starting at 0. Store in dp[i][j] how many paths reach cell (i, j), filling the table from the top-left, and return the count in the bottom-right corner.

def unique_paths(rows, cols):
    dp = table(rows, cols)
    # dp[i][j]: how many paths reach cell (i, j), stepping only right or down.
    # Fill every cell, then return the count in the bottom-right corner.
    return 0