-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14f0b68
commit 147f47c
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 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? | ||
|
||
# Dynamic Programming, boundary is important in this case. | ||
|
||
class Solution: | ||
# @return an integer | ||
def uniquePaths(self, m, n): | ||
steps = [[0 for x in range(n)] for x in range(m)] | ||
for j in range(n): | ||
steps[0][j] = 1 | ||
for j in range(m): | ||
steps[j][0] = 1 | ||
for i in range(1,m): | ||
for j in range(1,n): | ||
if i-1 < m and j-1 < n: | ||
steps[i][j] = steps[i-1][j] + steps[i][j-1] | ||
elif i-1<m: | ||
steps[i][j] = steps[i-1][j] | ||
elif j-1<n: | ||
steps[i][j] = steps[i][j-1] | ||
|
||
return steps[m-1][n-1] | ||
|