LeetCode - #63 Different Paths II

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

foreword

Our community will successively organize Gu Yi ( Netflix growth hacker, author of "The Way of the iOS Interview", ACE professional fitness coach. )'s Swift algorithm problem solutions into a text version for everyone to learn and read.

We have updated the LeetCode algorithm for 62 issues so far, and we will keep the update time and progress ( released at 9:00 am on Monday, Wednesday, and Friday ). There will be a big improvement.

If you don't accumulate a small step, you can't go a thousand miles; if you don't accumulate a small stream, you can't make a river. The Swift community accompanies you to move forward. If you have suggestions and comments, please leave a message at the end of the article, we will try our best to meet your needs.

Difficulty level: medium

1. Description

A robot is located in the upper left corner of a m x ngrid (the starting point is marked "Start" in the image below).

The robot can only move down or to the right one step at a time. The robot tries to reach the bottom right corner of the grid (labeled "Finish" in the image below).

Now consider that there are obstacles in the mesh. So how many different paths will there be from the top left corner to the bottom right corner?

Obstacles and empty positions in the grid are denoted by 1and 0.

2. Example

Example 1

输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出:2
解释:3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右
复制代码

Example 2

输入:obstacleGrid = [[0,1],[0,0]]
输出:1
复制代码

Restrictions:

  • m == obstacleGrid.length
  • n == obstacleGrid[i].length
  • 1 <= m, n <= 100
  • obstacleGrid[i][j]for 0or1

3. Answers

class UniquePathsII {
    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 dp = Array(repeating: Array(repeating: -1, count: n), count: m)
        
        return help(m - 1, n - 1, &dp, obstacleGrid)
    }
    
    fileprivate func help(_ m: Int, _ n: Int, _ dp: inout [[Int]], _ obstacleGrid: [[Int]]) -> Int {
        if m < 0 || n < 0 {
            return 0
        }
        if obstacleGrid[m][n] == 1 {
            return 0
        }
        if m == 0 && n == 0 {
            return 1
        }
        if dp[m][n] != -1 {
            return dp[m][n]
        }
        
        dp[m][n] = help(m - 1, n, &dp, obstacleGrid) + help(m, n - 1, &dp, obstacleGrid)
        return dp[m][n]
    }
}
复制代码
  • Main idea: 2D dynamic programming, which uses 2D arrays as caches to store computational data.
  • Time Complexity: O(mn)
  • Space Complexity: O(mn)

Repository for the algorithm solution: LeetCode-Swift

Click to go to LeetCode practice

about us

We are jointly maintained by Swift enthusiasts. We will share the technical content centered on Swift combat, SwiftUI, and Swift foundation, and also organize and collect excellent learning materials.

In the future, a lot of information will be translated to our official account. If you are interested, you can join us.

Guess you like

Origin juejin.im/post/7085504003659792421