Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Unique Paths

Dynamic Programmingmedium

Implement `uniquePaths(m, n)` returning the number of distinct paths from the top-left to the bottom-right of an `m` by `n` grid, moving only right or down.

Sample tests

Input: uniquePaths(3, 7)
Output: 28
Input: uniquePaths(3, 2)
Output: 3

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Recomputation without DP is exponential.

Learning resources

  • Wikipedia: Dynamic programming
Approach & explanation (try first)

Summing top and left contributions across a rolling row gives the count in O(m * n) time, O(n) space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.