15% off all plans & roadmaps Use code
See plansLeetCode 62 · Solution & editorial
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Unique Paths is a Dynamic Programming (DP) Patterns problem, in the 2D Array (Unique Paths on Grid) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
Count paths and find optimal paths in grids using 2D dynamic programming.
The reference solution runs in O(m * n) time and O(n) space.
Unique Paths has been reported in interviews at Amazon, Bloomberg, ByteDance, Facebook, Goldman Sachs, Google, Microsoft, Qualtrics.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Unique Paths is a Dynamic Programming (DP) Patterns problem, in the 2D Array (Unique Paths on Grid) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.