# Approach 1: Dynamic Programming to keep track of Path Sums

`class Solution(object):    def minPathSum(self, grid):        """        :type grid: List[List[int]]        :rtype: int        """        m = len(grid)        if m > 0:            n = len(grid)        else:            return 0        dp = [ * n for _ in range(m)]        dp = grid        for i in range(1, m):            dp[i] = grid[i] + dp[i-1]        for j in range(1, n):            dp[j] = grid[j] + dp[j-1]        for i in range(1, m):            for j in range(1, n):                dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])        return dp[m-1][n-1]`

We will initialize a 2D array named `dp` with zeros as always. The path sum for position (0,0) will obviously be `grid`. For the first row the sum to position `dp[j]` will be the sum of all `dp[k]` where `k < j`. Similarly for the first column the sum to position dp[i] will be the sum of all `dp[k] `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]`