LeetCode - 63. Unique Paths II

原创 2016年07月09日 18:32:42

这道题目与上一个题目是一样的,只是在matrix中有些小格子里有障碍,无法通行。基本的思路与上道题目是一样的,只是在初始化和进行DP的时候都要先进行一下检查,检查当前的访问到的[i][j] == 1,如果true则说明有障碍,将当前的[i][j]设置为0,如果false则按照原来的方式进行。DP的几个要素如下:

state: f[i][j]表示从左上角到当前的点有几种走法

function: f[i][j] = f[i - 1][j] + f[i][j - 1], 同时[i][0]和[0][j]初始化为1, 当然要注意有障碍的情况, 此时为0

initialize: 起点f[0][0]

answer: 终点f[m - 1][n - 1]

时间复杂度为O(n ^ 2),空间复杂度为O(1),因为题目中的矩阵是已经提供的,不需要再使用constant之外的额外空间,这道题目采用了一个技巧,就是直接在题目中给出的matrix上进行操作,而不是另外申请新的matrix[i][j],从而降低了空间复杂度,代码如下:

public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        // Corner case
        if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0) return 0;
        
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        
        // Check upper left corner
        if(obstacleGrid[0][0] == 1) return 0;
        else obstacleGrid[0][0] = 1;
        
        // Initialize side column and row
        for(int i = 1; i < m; i++){
            if(obstacleGrid[i][0] == 1) obstacleGrid[i][0] = 0;
            else obstacleGrid[i][0] = obstacleGrid[i - 1][0];
        }
        for(int j = 1; j < n; j++){
            if(obstacleGrid[0][j] == 1) obstacleGrid[0][j] = 0;
            else obstacleGrid[0][j] = obstacleGrid[0][j - 1];
        }
        
        // DP
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                if(obstacleGrid[i][j] == 1) obstacleGrid[i][j] = 0;
                else obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
            }
        }
        
        return obstacleGrid[m - 1][n - 1];
    }
}


知识点:
1. Matrix DP问题中,在题目中已经给出matrix的情况下,可以直接在matrix上面进行操作,这样就避免了额外空间的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode - 62,63. Unique Paths(II) & 64.Minimum Path Sum

算法系列博客之Dynamic Programming 本篇博客将运用动态规划的思想来解决leetcode上264号问题这三个题目的共同之处在于均是二维矩阵上的规划问题问题描述:62 Unique Pa...
  • Quiteen
  • Quiteen
  • 2017年06月11日 20:40
  • 94

LeetCode63——Unique Paths II

LeetCode63——Unique Paths II 跟LeetCode62——Unique Paths不同的是,这里是要在路径上设置障碍的。 因此不能用组合的方法去做了(也许可以,但我实在想不到)...

LeetCode_63---Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique...

LeetCode 64/62/63. Minimum Path Sum/ Unique Paths i, ii

1. 题目描述1.1 62 A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the dia...

Leetcode 63. Unique Paths II

// Follow up for "Unique Paths": // // Now consider if some obstacles are added to the grids. How ma...

[Leetcode刷题总结系列][Dynamic Programming]63. Unique Paths II

Unique Paths II Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. ...

Leetcode NO.63 Unique Paths II

题目要求如下: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids....

【leetcode】【63】Unique Paths II

一、问题描述 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. ...

【leetcode】63. Unique Paths II

一、题目描述 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m...

LeetCode-63. Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many uni...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode - 63. Unique Paths II
举报原因:
原因补充:

(最多只允许输入30个字)