[LeetCode] Unique Paths

0

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];
}

你可能感兴趣的

载入中...