一、问题描述
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]