15% off all plans & roadmaps Use code
See plansLeetCode 64 · Solution & editorial
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Minimum Path Sum 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(1) space.
Minimum Path Sum has been reported in interviews at Google, Amazon, Goldman Sachs, Facebook, Apple, Snapchat, Tesla.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Minimum Path Sum 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.