Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 64 Minimum Path Sum - Transition

Choose cheaper path

For cells not on the boundary: dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]. You pick the cheaper path (from above or left) and add the current cell's cost.

This greedy choice at each cell gives the global optimum because of optimal substructure. The answer is dp[m-1][n-1]. The transition is the core of any DP solution. You can solve this in O(1)O(1) space by overwriting the input grid in place.