内容中心

2026年退热贴水刺无纺布实力工厂推荐-常熟市永得利水刺无纺布有限公司

To solve this problem, we need to count the number of valid paths from the top-left corner to the bottom-right corner of a grid, where movement is restricted to right or down directions, and some cells are obstacles.

Approach

The problem can be efficiently solved using Dynamic Programming (DP) with space optimization. Here's the step-by-step approach:

  1. Edge Cases:

    • If the grid is empty or the starting cell (0,0) is an obstacle, return 0 immediately.
    • If the grid has only one cell (0,0) and it's not an obstacle, return 1.
  2. DP Initialization:

    • Use a 1D DP array where dp[j] represents the number of ways to reach the current row's j-th column. This reduces space complexity from O(mn) to O(n).
  3. First Row:

    • Fill the first row: since we can only move right, each cell in the first row is reachable only if there are no obstacles up to that point.
  4. Subsequent Rows:

    • For each row, update the DP array:
      • The first cell of the row is reachable only if it's not an obstacle and the cell above it (from the previous row) was reachable.
      • For other cells: if the cell is an obstacle, set dp[j] to 0. Otherwise, dp[j] is the sum of the value from the cell above (previous row's dp[j]) and the cell to the left (current row's dp[j-1]).

Solution Code

def uniquePathsWithObstacles(obstacleGrid):
    if not obstacleGrid or obstacleGrid[0][0] == 1:
        return 0

    m = len(obstacleGrid)
    n = len(obstacleGrid[0])

    dp = [0] * n
    dp[0] = 1  # Starting cell

    # Fill first row
    for j in range(1, n):
        if obstacleGrid[0][j] == 0:
            dp[j] = dp[j-1]
        else:
            dp[j] = 0

    # Fill remaining rows
    for i in range(1, m):
        # Update first column of current row
        if obstacleGrid[i][0] == 1:
            dp[0] = 0
        # Update other columns
        for j in range(1, n):
            if obstacleGrid[i][j] == 1:
                dp[j] = 0
            else:
                dp[j] += dp[j-1]

    return dp[-1]

Explanation

  • Space Optimization: Using a 1D array instead of a 2D array saves space. The 1D array is updated row by row, reusing the space from the previous row.
  • Efficiency: The time complexity is O(mn) (we traverse each cell once), and the space complexity is O(n) (size of the DP array).

This approach efficiently handles all cases, including obstacles blocking paths, and provides the correct number of valid paths.

Example Test Cases:

  1. Test Case 1:

    • Input: [[0,0,0],[0,1,0],[0,0,0]]
    • Output: 2 (valid paths: right→right→down→down, down→down→right→right, etc. → wait no, let's count: the valid paths are (0,0)→(0,1)→(0,2)→(1,2)→(2,2) and (0,0)→(1,0)→(2,0)→(2,1)→(2,2) → total of 2, which matches the output).
  2. Test Case 2:

    • Input: [[0,1],[0,0]]
    • Output: 1 (path: (0,0)→(1,0)→(1,1)).
  3. Test Case3:

    • Input: [[1]] → Output: 0.
    • Input: [[0]] → Output: 1.

This solution works for all these cases and more. It's optimal and handles all edge scenarios.

常熟市永得利水刺无纺布有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?