动态规划例题1

 62.不同路径:

# 62.不同路径
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        map = [[0 if i != 0 and j != 0 else 1 for j in range(n )] for i in range(m)]
        print(map)
        map[0][0]=0
        # print(map)

        for i in range(1, m ):
            for j in range(1, n ):
                map[i][j] = map[i - 1][j] + map[i][j - 1]
        print(map)

    def uniquePaths2(self, m: int, n: int) -> int:
        dp = [1] * n
        for _ in range(1, m):
            for i in range (1,n):
                dp[i]+=dp[i-1]
        return dp[n-1]



if __name__ == '__main__':
    s = Solution()
    result = s.uniquePaths2(3, 7)
    print(result)

你可能感兴趣的:(动态规划,python,算法)