LeetCode -- Minimum Path Sum

Question:

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.

Analysis:

给出一个由非负数构成的 m * n 的网格,找出里面一条从左上角到右下角的最短路径。

注意:在每次行走的过程中,你只能往下或往右走。

思路:

1. 因为之前写过迷宫的程序,所以首先想到用迷宫的思路即由于只能往右或者往下走,而根据贪心算法,每次我们肯定选择一个代价最小的方向走。所以一般情况下只有一种选择,但是当往右走往下走代价相同时,我们不知道接下来该往哪个方向走代价会最小,所以需要用栈记录此时的信息,得到所有可能的路径后,我们返回代价最小的那一条即可。

思路肯定是没错的,但是尼玛程序老实出错%>_<%。

2. 参考了一点网上的思路,用动态规划,申请一块额外的空间,这样除了第一行和第一列只有一种来源之外,其他的都可能来源于左方或上方,因此用动态规划可以很容易的解决。

Answer:

Dynamic Programing。

public class Solution {
    public int minPathSum(int[][] rid) {
        if(rid == null || (rid.length == 0 && rid[0].length == 0)) return 0;
        if(rid.length == 1 && rid[0].length == 1) return rid[0][0];
        int row = rid.length, col = rid[0].length;
        int[][] dp = new int[row][col];
        dp[0][0] = rid[0][0];
        for(int i=0; i<row; i++) {
            for(int j=0; j<col; j++) {
                if(i == 0 && j == 0)
                    continue;
                else if(i == 0 && j != 0)
                    dp[i][j] = dp[i][j-1] + rid[i][j];
                else if(j == 0 && i != 0)
                    dp[i][j] = dp[i-1][j] + rid[i][j];
                else {
                    dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j]) + rid[i][j];
                }
            }
        }
        return dp[row-1][col-1];
    }
}
时间: 2024-10-18 19:40:09

LeetCode -- Minimum Path Sum的相关文章

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

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 @ Python

原题地址:https://oj.leetcode.com/problems/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 r

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. SOLUTION 1: 相当基础

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. 首先每一个路径的上一个路径都是来自于其上方和左方 现将最上面的路径进行求和,最左边的路径进行求和

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 Minimum Path Sum (简单DP)

题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. 1 class Solution { 2 public: 3 int minPathSum(vector<vector<int>>& grid) { 4 int n=grid.size()-1; 5 int m=grid[n].size()-1; 6 for(int j=1; j<=m; j++) 7

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】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