Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Minimum Path Sum

Problem can be found in here!

Solution: Dynamic Programming

def minPathSum(grid: List[List[int]]) -> int:
    row_length, col_length = len(grid), len(grid[0])

    for row in range(row_length):
        for col in range(col_length):
            if row == 0 and col == 0:
                continue
            elif row == 0:
                grid[row][col] += grid[row][col-1]
            elif col == 0:
                grid[row][col] += grid[row-1][col]
            else:
                grid[row][col] += min(grid[row][col-1], grid[row-1][col])

    return grid[-1][-1]

Time Complexity: O(nm), Space Complexity: O(1), where n and m is the number of row and column, respectively.