zoukankan      html  css  js  c++  java
  • 64. Minimum Path Sum

    一个m*n矩阵,里面数字皆为非负整数,从左上到右下,问路径所经过的数的值之和的最小值

    """
    64. Minimum Path Sum
    Medium

    996

    25

    Favorite

    Share
    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.

    Note: You can only move either down or right at any point in time.

    Example:

    Input:
    [
    [1,3,1],
    [1,5,1],
    [4,2,1]
    ]
    Output: 7
    Explanation: Because the path 1→3→1→1→1 minimizes the sum.
    """

    到点(i,j)的值等于到(i,j-1),到(i-1,j)两个数的较小值加上该点的值

    class Solution:
        def minPathSum(self, grid):
            """
            :type grid: List[List[int]]
            :rtype: int
            """
            m, n = len(grid), len(grid[0])
            mat = [[0 for i in range(n)]for j in range(m)]
            mat[0][0] = grid[0][0]
            for j in range(1, n):
                mat[0][j] = mat[0][j-1] + grid[0][j]
            for i in range(1, m):
                mat[i][0] = mat[i-1][0] + grid[i][0]
            for i in range(1, m):
                for j in range(1, n):
                    mat[i][j] = min(mat[i-1][j], mat[i][j-1]) + grid[i][j]
            return mat[m-1][n-1]
  • 相关阅读:
    spring+mybatis+druid+xml
    springboot run(),bean注册
    linux命令之cat
    linux命令之more
    linux中配置maven环境
    linux中配置Java环境
    linux命令之nohup
    在Eclipse中创建Maven多模块工程的例子
    MINA之心跳协议运用
    Java动态代理
  • 原文地址:https://www.cnblogs.com/mangmangbiluo/p/10181848.html
Copyright © 2011-2022 走看看