码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode—Minimum Path Sum 二维数组最小路径,动态规划

时间:2015-04-08 18:03:40      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:leetcoed   动态规划   最小路径   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];
    }
};




LeetCode—Minimum Path Sum 二维数组最小路径,动态规划

标签:leetcoed   动态规划   最小路径   minimum path sum   

原文地址:http://blog.csdn.net/xietingcandice/article/details/44942969

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!