矩阵DP例题

120. 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).

/*
state:dp[i][j] 表示从(0,0)出发到(i,j)的最小路径长度
function:min(dp[i-1][j],dp[i-1][j-1])+triangle[i][j]
intialize:dp[0][0] = triangle[0][0] for(i=1->m-1) dp[i][0] = dp[i-1][0] + triangle[i][0] dp[i][i] = dp[i-1][i-1]+triangle[i][i]
answer:min(dp[m-1][i]) for i=0->m-1    
*/ 
class Solution{
    public int minimumTotal(List<List<Integer>> triangle){
        if(triangle == null || triangle.get(0).size() == 0)
            return -1;
        if(triangle.get(0) == null || triangle.get(0).size() == 0)
            return -1;
        int m = triangle.size();
        int[][] dp = new int[m][m];
        dp[0][0] = triangle.get(0).get(0);
        //初始化边界值
        for(int i = 1; i < m;i++){
            dp[i][0] = dp[i-1][0] + triangle.get(i).get(0);//左边界初始化
            dp[i][i] = dp[i-1][i-1]+triangle.get(i).get(i);//右边界初始化
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < i; j++){
                dp[i][j] = Math.min(dp[i-1][j],dp[i-1][j-1]) + triangle.get(i).get(j);
            }
        }
        //遍历最后一层,取出最小值
        int minValue = Integer.MAX_VALUE;
        for(int i = 0; i < m; i++)
            minValue = Math.min(minValue,dp[m-1][i]);
        return minValue;
    }
    
}

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

Example 1:

[[1,3,1],
 [1,5,1],
 [4,2,1]]
Given the above grid map, return  7 . Because the path 1→3→1→1→1 minimizes the sum.

/*
state:dp[x][y] 从起点走到(x,y)的最短路径
function:dp[x][y] = min(dp[x-1][y],dp[x][y-1])+grid[x][y]
intialize:dp[0][0] = grid[0][0]
          dp[i][0] = grid[i][0] + dp[i-1][0] for(i = 1....m) 
          dp[0][i] = grid[0][i] + dp[0][i-1] for(i = 1....n)
answer:dp[m][n]         
*/
class Solution {
    public int minPathSum(int[][] grid) {
        if(grid == null || grid.length == 0)
            return -1;
        if(grid[0] == null || grid[0].length == 0)
            return -1;
        int m = grid.length,n = grid[0].length;
        int[][] dp = new int[m][n];
        // intialize:
        dp[0][0] = grid[0][0];
        for(int i = 1; i < m; i++)
            dp[i][0] = grid[i][0] + dp[i-1][0];
        for(int i = 1; i < n; i++){
            dp[0][i] = grid[0][i] + dp[0][i-1];
        }
        
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1])+grid[i][j];
            }
        }
        return dp[m-1][n-1];
        
    }
}


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

/*
state:dp[x][y] 从起点走到(x,y)的可能路径的条数
function:dp[x][y] = dp[x][y-1] + dp[x-1][y]
intialize:dp[0][0] = 1
          dp[i][0] = 1 for(i = 1....m) 
          dp[0][i] = 1 for(i = 1....n)
answer:dp[m][n]         
*/
class Solution {
    public int uniquePaths(int m, int n) {
        if(m == 0 || n == 0) return -1;
        int[][] dp = new int[m][n];
        
        //intialize
        dp[0][0] = 1;
        for(int i = 1; i < m; i++)
            dp[i][0] = dp[i-1][0];
        for(int j = 1; j < n; j++)
            dp[0][j] = dp[0][j-1];
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
}


63. Unique Paths II

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.

/*
state:dp[x][y] 从起点走到(x,y)的可能路径的条数
function:dp[i][j] = (obstacleGrid[i][j] == 1)?0:dp[i-1][j] + dp[i][j-1];
intialize:dp[0][0] = (obstacleGrid[0][0] == 1)?0:1;
          dp[i][0] = (obstacleGrid[i][0] == 1)?0:dp[i-1][0]; for(i = 1....m) 
          dp[0][i] = (obstacleGrid[0][i] == 1)?0:dp[0][i-1]; for(i = 1....n)
answer:dp[m][n]         
*/
class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid == null || obstacleGrid.length == 0) return -1;
        if(obstacleGrid[0] == null || obstacleGrid[0].length == 0) return -1;
        int m = obstacleGrid.length,n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        //intialize
        dp[0][0] = (obstacleGrid[0][0] == 1)?0:1;
        for(int i = 1; i < m; i++)
            dp[i][0] = (obstacleGrid[i][0] == 1)?0:dp[i-1][0];
        for(int j = 1; j < n; j++)
            dp[0][j] = (obstacleGrid[0][j] == 1)?0:dp[0][j-1];
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = (obstacleGrid[i][j] == 1)?0:dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值