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.

首先每一个路径的上一个路径都是来自于其上方和左方

现将最上面的路径进行求和,最左边的路径进行求和(这里没有直接求和的原因是因为用一个一维数组res[n]记录路径,这里比较巧妙得节约了空间,所以将最左边的求和算法放到了循环里面  res[i][j] = min(res[i-1][j],res[i][j-1])+val[i][j]
 )

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        if(grid.empty() || grid[0].empty())
        {
            return 0;
        }
        int m = grid.size();
        int n = grid[0].size();
        int *res = new int[n];
        res[0] = grid[0][0];
        for(int i = 1;i < n; i++)
        {
            res[i] = res[i-1]+grid[0][i];
        }
        for(int i = 1; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if (0 == j)
                {
                    res[j] += grid[i][0];
                }
                else
                {
                    res[j] = min(res[j-1],res[j])+grid[i][j];
                }

            }
        }
        int result = res[n-1];
        delete []res;
        return result;
    }
};

和这种动态规划类似的算法还有:

Unique Paths

https://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 reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

class Solution {
public:
    int uniquePaths(int m, int n) {
        if(m == 0 || n == 0)
        {
            return 0;
        }
        int res[100][100];
        for(int i = 0; i < n; i++)
        {
            res[0][i] = 1; //<因为只有一种方法
        }
        for(int i = 0; i < m; i++)
        {
            res[i][0] = 1;
        }
        for(int i = 1; i < m; i++)
        {
            for(int j = 1; j < n; j++)
            {
                res[i][j] = res[i-1][j]+res[i][j-1];
            }
        }
        return res[m-1][n-1];
    }
};

如果只是利用一维空间:

class Solution {
public:
    int uniquePaths(int m, int n) {
        if(m <= 0 || n <= 0
        {
            return 0;
        }
        int res[100] = {0};
        res[0] = 1;
        for(int i = 0; i < m; i++)
        {
            for(int j = 1; j < n; j++)
            {
                res[j] += res[j-1];
            }
        }
        return res[n-1];
    }
};

Unique Paths II

这里涉及到一些障碍物的情况,如果值为1表示这里有障碍物,不能跨越

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

Note: m and n will
be at most 100.

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
        if(obstacleGrid.empty() || obstacleGrid[0].empty())
        {
            return 0;
        }
        int res[100] = {0};//<还是采用一维数组
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        res[0] = (obstacleGrid[0][0] != 1);
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                <span style="color:#ff0000;">if(j == 0)
                {
                    if(obstacleGrid[i][j] == 1)  //<这里需要稍微注意一下如果当前有障碍表示为0,否则可以延续之前的状态
                    {
                        res[j] = 0;
                    }
                }</span>
                else
                {
                    <span style="color:#ff0000;">if(obstacleGrid[i][j] == 1)
                    {
                        res[j] = 0;
                    }
                    else
                    {
                        res[j] += res[j-1];
                    }</span>
                }
            }
        }
        return res[n-1];
    }
};

时间: 2024-10-24 07:54:40

LeetCode—Minimum Path Sum 二维数组最小路径,动态规划的相关文章

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 &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 @ 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&mdash;&mdash;Rotate Image(二维数组顺时针旋转90度)

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 分析: 二维数组a[n][n]顺时针旋转90度,要解决这个问题,无疑,第一件事儿就是找规律. 当n=1时,不用动了. 当n=2时, 旋转之后变为 有: a[0][0] = a[1][0] a[1][0] = a[1

[leetcode]54. Spiral Matrix二维数组螺旋取数

import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n columns), * return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You

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

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

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