-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrid_unique_paths.py
More file actions
executable file
·35 lines (23 loc) · 922 Bytes
/
Copy pathgrid_unique_paths.py
File metadata and controls
executable file
·35 lines (23 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# coding=utf-8
"""
A robot is located at the top-left corner of an A x B grid (marked ‘Start’ in the diagram below).
Path Sum: Example 1
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?
Note: A and B will be such that the resulting answer fits in a 32 bit signed integer.
Example :
Input : A = 2, B = 2
Output : 2
2 possible routes : (0, 0) -> (0, 1) -> (1, 1)
OR : (0, 0) -> (1, 0) -> (1, 1)
"""
import math
class Solution:
# @param A : integer
# @param B : integer
# @return an integer
def uniquePaths(self, A, B):
return (math.factorial(A + B - 2)) / (math.factorial(B - 1) * math.factorial(A - 1))
s = Solution()
assert s.uniquePaths(4, 5) == 35, "Houston, we have a problem."
print "Success!"