64. Minimum Path Sum
Approach 1: Dynamic Programming to keep track of Path Sums
We will initialize a 2D array named dp
with zeros as always. The path sum for position (0,0) will obviously be grid[0][0]
. For the first row the sum to position dp[0][j]
will be the sum of all dp[0][k]
where k < j
. Similarly for the first column the sum to position dp[i][0] will be the sum of all dp[k][0]
where k < i
.
After that we simply have to iterate through the array and make sure each time to update the path sum by adding grid[i][j]
to the minimum of dp[i-1][j]
and dp[i][j-1]
and store it in dp[i][j]
Last updated