LEETCODE-DAY39


title: LEETCODE-DAY39

date: 2024-03-30 17:24:46
tags:

今日内容:62.不同路径、63. 不同路径 II

T1

python 复制代码
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp=[[0 for _ in range(n+1)]for _ in range(m+1)]
        dp[1][1]=1
        for i in range(1,m+1):
            for j in range(1,n+1):
                dp[i][j]=dp[i-1][j]+dp[i][j-1]

        return dp[m][n]

输入

m =

3

n =

2

输出

0

预期结果

3

dp11被覆盖了

python 复制代码
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp=[[0 for _ in range(n+1)]for _ in range(m+1)]
        dp[1][1]=1
        for i in range(1,m+1):
            for j in range(1,n+1):
                if i==1 and j==1:
                    continue
                dp[i][j]=dp[i-1][j]+dp[i][j-1]

        return dp[m][n]

AC

T2

python 复制代码
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m=len(obstacleGrid)
        n=len(obstacleGrid[0])
        dp=[[0 for _ in range(n)]for _ in range(m)]
        for i in range(m):
            dp[i][0]=1
        for j in range(n):
            dp[0][j]=1

        for i in range(1,m):
            for j in range(1,n):
      
                dp[i][j]=dp[i-1][j]+dp[i][j-1]
        res=dp[m-1][n-1]
        for i in range(m):
            for j in range(n):
                if obstacleGrid[i][j]==1:
                    res-=dp[i][j]*dp[m-i-1][n-j-1]
                    
        return res

输入

obstacleGrid =

\[1,1\]

输出

-1

预期结果

0

python 复制代码
#同上
return max(res,0)

输入

obstacleGrid =

\[0,0,0,0\],\[0,1,0,0\],\[0,0,0,0\],\[0,0,1,0\],\[0,0,0,0\]

输出

0

预期结果

7

需要换一种思路

python 复制代码
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m=len(obstacleGrid)
        n=len(obstacleGrid[0])
        dp=[[0 for _ in range(n)]for _ in range(m)]
        if obstacleGrid[0][0]==1:
            return 0
        for i in range(m):
            if obstacleGrid[i][0]==1:
                dp[i][0]=0
            else:
                dp[i][0]=1
        for j in range(n):
            if obstacleGrid[0][j]==1:
                dp[0][j]=0
            else:
                dp[0][j]=1


        for i in range(1,m):
            for j in range(1,n):
                if obstacleGrid[i][j]==1:
                    dp[i][j]==0
                else:
                   dp[i][j]=dp[i-1][j]+dp[i][j-1]
       
       
        return dp[m-1][n-1]

输入

obstacleGrid =

\[0,0\],\[1,1\],\[0,0\]

输出

1

预期结果

0

初始化有问题

第一行遇到障碍后障碍之后的列也应初始化为0

python 复制代码
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m=len(obstacleGrid)
        n=len(obstacleGrid[0])
        dp=[[0 for _ in range(n)]for _ in range(m)]
        if obstacleGrid[0][0]==1:
            return 0
        for i in range(m):
            if obstacleGrid[i][0]==1:
                dp[i][0]=0
            elif i>0 and dp[i-1][0]==0:
                dp[i][0]=0
            else:
                dp[i][0]=1
        for j in range(n):
            if obstacleGrid[0][j]==1:
                dp[0][j]=0
            elif j>0 and dp[0][j-1]==0:
                dp[0][j]=0
            else:
                dp[0][j]=1


        for i in range(1,m):
            for j in range(1,n):
                if obstacleGrid[i][j]==1:
                    dp[i][j]==0
                else:
                   dp[i][j]=dp[i-1][j]+dp[i][j-1]
       
       
        return dp[m-1][n-1]

AC

python 复制代码
python 复制代码
相关推荐
To_OC2 小时前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
Scott9999HH2 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
2401_841495643 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
AI云海3 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰4 小时前
[爬虫]-Urllib
爬虫·python
玉鸯5 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260856 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao6 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest