[LeetCode] Unique Paths

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?

DP

Time Complexity
O(mn)
Space Complexity
O(mn)

思路

matrixm means the possible unique paths to matrixi. The base case is the first row and first col. Because the robot can only move either down or right so the first row and first col of the matrix should all be 1. The dp funciton should be matrixi = matrixi - 1 + matrixi.

代码

public int uniquePaths(int m, int n) {
    if(m == 0 && n == 0) return 0;
    int[][] matrix = new int[m][n];
    matrix[0][0] = 0;
    for(int i = 0; i < m; i++){
        matrix[i][0] = 1;
    }
    for(int j = 0; j < n; j++){
        matrix[0][j] = 1;
    }
    for(int i = 1; i < m; i++){
        for(int j = 1; j < n; j++){
            matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1];
        }
    }
    return matrix[m - 1][n - 1];
}

优化

Time Complexity
O(mn)
Space Complexity
O(n)

思想

Because we only need the information from up and left, so we can only keep one array.
To minimize the space complexity, we can make an array's length which is min(m,n). The base case is the first row or first col which equals one. res[j] = res[j]from left + res[j - 1]from up

代码

public int uniquePaths(int m, int n) {
    //corner case
    if(m == 0 && n == 0) return 0;
    
    int[] res = new int[n];
    
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            if(j == 0) res[j] = 1;
            else res[j] = res[j] + res[j - 1];
        }
    }
    return res[n - 1];
}

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.

DP

Time Complexity
O(mn)
Space Complexity
O(mn)

思路

It is the same as the Unique I, but there are walls in matrix. So for base case, if we meet one wall, the left position of 0 in the same row or col can't be reached so just break the for loop. Except for the base cases, if it is a wall , there is no path to reach, so set numPathi to 0,
otherwise numPath[i][j] = numPath[i - 1][j] + numPath[i][j - 1].

代码

public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    //corner case
    if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0] == null || obstacleGrid[0].length == 0) return 0;
    int rows = obstacleGrid.length, cols = obstacleGrid[0].length;
    int[][] numPath = new int[rows][cols];
    
    //initilize the first col
    for(int i = 0; i < rows; i++){
        if(obstacleGrid[i][0] == 1){
            break;
        }else{
            numPath[i][0] = 1;
        }
    }
    //initilize the first row
    for(int j = 0; j < cols; j++){
        if(obstacleGrid[0][j] == 1){
            break;
        }else{
            numPath[0][j] = 1;
        }
    }
    
    for(int i = 1; i < rows; i++){
        for(int j = 1; j < cols; j++){
            numPath[i][j] = obstacleGrid[i][j] == 1 ? 0 : numPath[i- 1][j] + numPath[i][j - 1];
        }
    }
    
    return numPath[rows - 1][cols - 1];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值