LeetCode 62/63/120/64 Unique PathsI/II Triangle/Min sum Path/Rectangle Area--DP

一: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;
        
    }
};

四: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.

链接:https://leetcode.com/problems/minimum-path-sum/

代码:

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        int m = grid.size();
        if(m == 0) return 0;
        int n = grid[0].size();
        if(n == 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] = grid[m-1][n-1];
        for(int i = m-2; i >= 0; i--){                  // 动态规划 初始化
            path[i][n-1] = path[i+1][n-1] + grid[i][n-1];
        }
        for(int i = n-2; i >= 0; i--){
            path[m-1][i] = path[m-1][i+1]+ grid[m-1][i];
        }
        for(int i = m-2; i >= 0; i--){              // 动态规划迭代
            for(int j = n-2; j >= 0; j--)
                path[i][j] = grid[i][j] + min(path[i+1][j], path[i][j+1]);
        }
        int result = path[0][0];
        for(int i = 0; i < m; i++)
            delete []path[i];
        delete []path;
        return result;
        
        
    }
};

五:Rectangle Area

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.

分析:此题为动态规划问题,时间复杂度肯定是O(M*N),空间复杂度可以降低为O(min{M,N})。迭代式为square[i][j] = min(min(square[i][j+1],square[i+1][j]), square[i+1][j+1]);

代码:

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int m = matrix.size();
        if(m == 0) return 0;
        int n = matrix[0].size();
        int **square = new int*[m];
        for(int i = 0; i < m; i++){
            square[i] = new int[n];
            memset(square[i], 0, sizeof(int)*n);
        }
        int maxSquare = 0;
        for(int i = 0; i < m; i++){
            square[i][n-1] = matrix[i][n-1]-'0';
            maxSquare = max(maxSquare, square[i][n-1]);
        }
        for(int i = 0; i < n; i++){
            square[m-1][i] = matrix[m-1][i] - '0';
            maxSquare = max(maxSquare, square[m-1][i]);
        }
        
        for(int i = m-2; i >= 0; i--){
            for(int j = n-2; j >= 0; j--){
                if(matrix[i][j] == '1')
                    square[i][j] =min(min(square[i+1][j], square[i][j+1]), square[i+1][j+1])+1;
                else square[i][j] = 0;
                maxSquare = max(maxSquare, square[i][j]);
            }
        }
        return maxSquare*maxSquare;
    }
};

下面给出一份O(N)空间复杂度的代码:

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int m = matrix.size();
        if(m == 0) return 0;
        int n = matrix[0].size();
        int **square = new int*[2];
        for(int i = 0; i < 2; i++){
            square[i] = new int[n];
            memset(square[i], 0, sizeof(int)*n);
        }
        int maxSquare = 0;
       /* for(int i = 0; i < 2; i++){
            square[i][n-1] = matrix[i][n-1]-'0';
            maxSquare = max(maxSquare, square[i][n-1]);
        }*/
        for(int i = 0; i < n; i++){
            square[0][i] = matrix[m-1][i] - '0';
            maxSquare = max(maxSquare, square[0][i]);
        }
        
        for(int i = m-2; i >= 0; i--){
            square[1][n-1] = matrix[i][n-1]-'0';
            maxSquare = max(maxSquare, square[1][n-1]);
            int j = n-2;
            for(j = n-2; j >= 0; j--){
                if(matrix[i][j] == '1')
                    square[1][j] =min(min(square[0][j], square[1][j+1]), square[0][j+1])+1;  // 下、右、右下三个的最小者
                else square[1][j] = 0;
                maxSquare = max(maxSquare, square[1][j]);
                if(j+2 < n) square[0][j+2] =  square[1][j+2];
            }
            if(j+2 < n) square[0][j+2] = square[1][j+2];
            square[0][0] = square[1][0];
        }
        return maxSquare*maxSquare;
    }
};



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值