[go: nahoru, domu]

Skip to content

Latest commit

 

History

History
81 lines (65 loc) · 2.19 KB

63-Unique-Paths-II.md

File metadata and controls

81 lines (65 loc) · 2.19 KB

63. Unique Paths II

2020-07-30

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

Now consider if some obstacles are added to the grids. How many unique paths would there be?

img

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

Solution

class Solution {

    func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
        let m = obstacleGrid.count
        guard m > 0 else {
            return 0
        }
        let n = obstacleGrid[0].count
        guard n > 0 else {
            return 0
        }
        var cache = [[Int]].init(repeating: [Int].init(repeating: 0, count: n), count: m)
     
        for y in 1...m {
            for x in 1...n {
                if obstacleGrid[y - 1][x - 1] == 1 {
                    cache[y - 1][x - 1] = 0
                } else if x == 1 {
                    cache[y - 1][x - 1] = 1
                    for _y in 1...y {
                        if obstacleGrid[_y - 1][x - 1] == 1 {
                            cache[y - 1][x - 1] = 0
                            break
                        }
                    }
                } else if y == 1 {
                    cache[y - 1][x - 1] = 1
                    for _x in 1...x {
                        if obstacleGrid[y - 1][_x - 1] == 1 {
                            cache[y - 1][x - 1] = 0
                            break
                        }
                    }
                } else {
                    cache[y - 1][x - 1] = cache[y - 2][x - 1] + cache[y - 1][x - 2]
                }
            }
        }
        return cache[m - 1][n - 1]
    }
}