码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 62/63/120 Unique PathsI/II Triangle--DP

时间:2015-04-06 17:21:59      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:array   dp   leetcode   

一:unique Path

题目:

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.

链接:https://leetcode.com/problems/unique-paths/

分析:此题很明显是动态规划问题,用F[m][n],其中F[i][j]表示在(i,j)位置时的最大方案数,他就等于F[i+1][j]+F[i][j+1]. 

代码:

class Solution {
public:
    int uniquePaths(int m, int n) {
        int **path = new int*[m];
        for(int i = 0; i < m; i++){
            path[i] = new int[n];
            memset(path[i], 0, sizeof(int)*n);
        }
        for(int i = 0; i < m; i++)         // 初始化
            path[i][n-1] = 1;
        for(int i = 0; i < n; i++)
            path[m-1][i] = 1;
        for(int i = m-2; i >=0; i--){
            for(int j = n-2; j >= 0; j--){
                path[i][j] = path[i+1][j] + path[i][j+1];   // DP算法
            }
        }
        int pathes = path[0][0];
        for(int i = 0; i < m; i++)
            delete []path[i];
        delete []path;
        return pathes;
        
    }
};

二:Unique PathII

题目:

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

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


链接:https://leetcode.com/problems/unique-paths-ii/

分析:此题就是在上题中增加了一些障碍物,这对初始化会带来一定影响。

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        if(obstacleGrid[m-1][n-1] || obstacleGrid[0][0]) return 0;
        int **path = new int*[m];
        for(int i = 0; i < m; i++){
            path[i] = new int[n];
            memset(path[i], 0, sizeof(int)*n);
        }
        path[m-1][n-1] = 1;
        
        for(int i = m-2; i >= 0; i--){         // 有障碍物的初始化 对最后一行最后一列初始化
            if(obstacleGrid[i][n-1]) path[i][n-1] = 0;
            else path[i][n-1] = path[i+1][n-1];
            
        }
        for(int i = n-2; i >= 0; i--)
            if(obstacleGrid[m-1][i]) path[m-1][i] = 0;
            else path[m-1][i] = path[m-1][i+1];
            
            
        for(int i = m-2; i >=0; i--){
            for(int j = n-2; j >= 0; j--){
                if(obstacleGrid[i][j]) path[i][j] = 0;
                else path[i][j] = path[i+1][j] + path[i][j+1];   // DP算法
            }
        }
        int pathes = path[0][0];
        for(int i = 0; i < m; i++)
            delete []path[i];
        delete []path;
        return pathes;
        
    }
};
三:Triangle

题目:

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

链接:https://leetcode.com/problems/triangle/

分析:用F[i]表示位置为i时的最小和,他就=triangle[i]+min(F[i], F[i+1])  典型的DP

代码:

class Solution {
public:
    int minimumTotal(vector<vector<int> > &triangle) {
        int lastCols = triangle[triangle.size()-1].size();
        int *f = new int[lastCols];
        for(int i = 0; i < lastCols; i++){
            f[i] = triangle[triangle.size()-1][i];         // 初始化
        }
        for(int i = triangle.size()-2; i >=0; i--){
            for(int j = 0; j < triangle[i].size(); j++){
                f[j] = triangle[i][j] + min(f[j], f[j+1]);       // 递归式
            }
        }
        int result = f[0];
        delete []f;
        return result;
        
    }
};



LeetCode 62/63/120 Unique PathsI/II Triangle--DP

标签:array   dp   leetcode   

原文地址:http://blog.csdn.net/lu597203933/article/details/44902585

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