Skip to content

Commit b6c99b2

Browse files
committed
Leetcode 62
Python Solution
1 parent 1e915a3 commit b6c99b2

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
4747
| 54 | Medium | [Spiral Matrix](leetcode/54-Medium-Spiral-Matrix/problem.md) | [Python](leetcode/54-Medium-Spiral-Matrix/answer.py) |
4848
| 56 | Medium | [Merge Intervals](leetcode/56-Medium-Merge-Intervals/problem.md) | [Python](leetcode/56-Medium-Merge-Intervals/answer.py) |
4949
| 58 | Easy | [Length of Last Word](leetcode/58-Easy-Length-Of-Last-Word/problem.md) | [Python](leetcode/58-Easy-Length-Of-Last-Word/answer.py) |
50+
| 62 | Medium | [Unique Paths](leetcode/62-Medium-Unique-Paths/problem.md) | [Python](leetcode/62-Medium-Unique-Paths/answer.py) |
5051
| 66 | Easy | [Plus One](leetcode/66-Easy-Plus-One/problem.md) | [Python](leetcode/66-Easy-Plus-One/answer.py) |
5152
| 67 | Easy | [Add Binary](leetcode/67-Easy-Add-Binary/problem.md) | [Python](leetcode/67-Easy-Add-Binary/answer.py) |
5253
| 69 | Easy | [Sqrt()](leetcode/69-Easy-Sqrt/problem.md) | [Python](leetcode/69-Easy-Sqrt/answer.py) |

Diff for: leetcode/62-Medium-Unique-Paths/answer.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/python3
2+
3+
#------------------------------------------------------------------------------
4+
5+
class Solution:
6+
def uniquePaths(self, m, n):
7+
"""
8+
:type m: int
9+
:type n: int
10+
:rtype: int
11+
"""
12+
# Create the grid to keep track of paths
13+
grid = [[0]*n for _ in range(m)]
14+
# Iterate through each path
15+
for i in range(m):
16+
for j in range(n):
17+
# If start (i==0 or j==0) set to 1
18+
if not i or not j:
19+
grid[i][j] = 1
20+
else:
21+
# Sum up the possible paths reaching this spot
22+
grid[i][j] = grid[i-1][j] + grid[i][j-1]
23+
return grid[m-1][n-1]
24+
25+
26+
#------------------------------------------------------------------------------
27+
#Testing

Diff for: leetcode/62-Medium-Unique-Paths/problem.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Problem 62: Unique Paths
2+
3+
4+
#### Difficulty: Medium
5+
6+
#### Problem
7+
8+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
9+
10+
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).
11+
12+
How many possible unique paths are there?
13+
14+
#### Example
15+
16+
<pre>
17+
18+
Input: m = 3, n = 2
19+
Output: 3
20+
21+
Explanation:
22+
23+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
24+
25+
1. Right -> Right -> Down
26+
27+
2. Right -> Down -> Right
28+
29+
3. Down -> Right -> Right
30+
31+
</pre>
32+

0 commit comments

Comments
 (0)