lintcode 容易题:Minimum Path Sum 最小路径和

题目:

最小路径和

给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径。

样例

注意

你在同一时间只能向下或者向右移动一步

解题:

这个和求三角形的最小路径的差不多,这里是个矩阵,第一列和第一行要单独处理,每一点的值等于自身的值加上上一点的值,对于中间节点:grid[i][j] + = min( grid[i-1][j] , grid[i][j-1]) ,也就是说,i j 点的值只能是其左侧的点或者上侧的点加过来,这个时间复杂度比较高O(N2)

Java程序:

public class Solution {
    /**
     * @param grid: a list of lists of integers.
     * @return: An integer, minimizes the sum of all numbers along its path
     */
    public int minPathSum(int[][] grid) {
        // write your code here
        int m = grid.length;
        if(m == 0 )
            return 0;
        int n = grid[0].length;
        if( n==0 )
            return 0;
        for(int i=1 ; i<n; i++ ){
            grid[0][i] += grid[0][i-1];
        }
        for( int i= 1;i< m; i++ ){
            grid[i][0] += grid[i-1][0];
        }
        for(int i=1; i< m ; i++){
            for(int j = 1;j< n; j++){
                grid[i][j] += Math.min(grid[i-1][j],grid[i][j-1]);
            }
        }
        return grid[m-1][n-1];
    }
}

Python程序:

class Solution:
    """
    @param grid: a list of lists of integers.
    @return: An integer, minimizes the sum of all numbers along its path
    """
    def minPathSum(self, grid):
        # write your code here
        m = len(grid)
        if m == 0:
            return 0
        n = len(grid[0])
        if n==0:
            return 0
        for i in range(m):
            for j in range(n):
                if i==0 and j!=0:
                    grid[i][j] += grid[i][j-1]
                elif j==0 and i!=0:
                    grid[i][j] += grid[i-1][j]
                elif j!=0 and i!=0:
                    grid[i][j] += min(grid[i-1][j] , grid[i][j-1])
        return grid[m-1][n-1]

时间: 2024-12-20 08:49:46

lintcode 容易题:Minimum Path Sum 最小路径和的相关文章

Leetcode:Minimum Path Sum 最小路径和

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. 此题解法同Triangle 数字三角形.在此不再赘述. class

leetcode_64题——Minimum Path Sum(动态规划)

Minimum Path Sum Total Accepted: 38669 Total Submissions: 120082My Submissions Question Solution 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. Not

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: 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. 解题分析: 每次只能向下或者向

LeetCode之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: 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 righ

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum 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. 这道题的要求是在m*n

[LeetCode] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to rea

Leetcode 动态规划 Minimum Path Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Minimum Path Sum Total Accepted: 15789 Total Submissions: 50645My Submissions Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

【Leetcode】Minimum Path Sum

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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return