leetcode62&63&64_Unique Paths

一、问题描述

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

How many possible unique paths are there?
这里写图片描述

二、代码编写

很明显这是个dp问题

dp[x,0]=1 & dp[0,x]=1  # boundary condition
dp[x,y] = dp[x-1,y]+dp[x,y-1]  x>0&y>0  # iteration

这里写图片描述
实现代码如下

'''
@ author: wttttt at 2016.12.20
@ problem description see: https://leetcode.com/problems/unique-paths/
@ solution explanation see: http://blog.csdn.net/u014265088
@ github:https://github.com/wttttt-wang/leetcode
@ dynamic programming
@ time complexity: O(n*m), space complexity O(n*m)
'''

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        """
        dp
        dp[x,0]=1 & dp[0,x]=1
        dp[x,y] = dp[x-1,y]+dp[x,y-1]  x>0&y>0
        """
        if m==0 or n == 0:
            return 0
        # initialize for dp
        dp = [[0 for j in range(n)] for i in range(m)]
        # boundary condition
        for i in range(m):
            dp[i][0] = 1
        for i in range(n):
            dp[0][i] = 1
        for i in range(1, m):
            for j in range(1, n):
                dp[i][j] = dp[i-1][j] + dp[i][j-1]
        return dp[m-1][n-1]

一、问题描述

Follow up for “Unique Paths”:

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

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

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.

Note: m and n will be at most 100.

二、代码编写

思想跟62题一样,只是多了障碍。只需要将有障碍的位置设为0就可以了。dp的时候要先判断,如果当前位置有障碍,直接将其值设为0。

# boundary condition
dp[x,0]=dp[x-1,0] if dp[x,0]!=1 else 0
dp[0,x]=dp[0,x-1] if dp[0,x]!=1 else 0
# iteration
dp[x,y] = dp[x-1,y]+dp[x,y-1] if dp[x,y]!=0 else 0 

实现的代码如下:

'''
@ author: wttttt at 2016.12.21
@ problem description see: https://leetcode.com/problems/unique-paths-ii/
@ solution explanation see: http://blog.csdn.net/u014265088/article/details/53769588
@ github:https://github.com/wttttt-wang/leetcode
@ dynamic programming
@ time complexity: O(n*m), space complexity O(1)[in place]
'''
class Solution(object):
    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        m = len(obstacleGrid)
        n = len(obstacleGrid[0]) if m > 0 else 0
        if m == 0 or n == 0:
            return 0
        if obstacleGrid[0][0] == 1:
            return 0
        # boundary condition
        obstacleGrid[0][0] = 1
        for i in range(1, m):
            obstacleGrid[i][0] = obstacleGrid[i - 1][0] if obstacleGrid[i][0] != 1 else 0
        for i in range(1, n):
            obstacleGrid[0][i] = obstacleGrid[0][i - 1] if obstacleGrid[0][i] != 1 else 0
        # iteration
        for i in range(1, m):
            for j in range(1, n):
                if obstacleGrid[i][j] == 1:
                    obstacleGrid[i][j] = 0
                else:
                    obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
        return obstacleGrid[m - 1][n - 1]

一、问题描述

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

二、代码编写

还是dp的思想,基本跟62和63题没什么区别,就是迭代的公式做细微的调整,如下:

dp[x,0]=dp[x-1,0]+grid[x,0] & dp[0,x]=dp[0,x-1]+grid[0,x]
dp[x,y] = grid[x,y] + min(dp[x-1,y], dp[x,y-1])  x>0&y>0

具体代码如下:

'''
@ author: wttttt at 2016.12.22
@ problem description see: https://leetcode.com/problems/minimum-path-sum/
@ solution explanation see: http://blog.csdn.net/u014265088/article/details/53769588
@ github:https://github.com/wttttt-wang/leetcode
@ dynamic programming
@ time complexity: O(n*m), space complexity O(n*m)
'''
class Solution(object):
    def minPathSum(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        """
        almost the same with leetcode_62&63, dp
        dp
        dp[x,0]=dp[x-1,0]+grid[x,0] & dp[0,x]=dp[0,x-1]+grid[0,x]
        dp[x,y] = grid[x,y] + min(dp[x-1,y], dp[x,y-1])  x>0&y>0
        """
        m = len(grid)
        n = len(grid[0]) if m>0 else 0
        if m == 0 or n == 0:
            return 0
        dp = [[0 for i in range(n)] for j in range(m)]
        dp[0][0] = grid[0][0]
        for i in range(1, m):
            dp[i][0] = dp[i-1][0] + grid[i][0]
        for i in range(1, n):
            dp[0][i] = dp[0][i-1] + grid[0][i]
        for i in range(1, m):
            for j in range(1, n):
                dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])
        return dp[m-1][n-1]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值