Created
June 7, 2017 18:04
-
-
Save prkstaff/1b711a99c8148d588318e465b3d88eb9 to your computer and use it in GitHub Desktop.
Unique Paths in a Grid
This file contains hidden or 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
import copy | |
#Unique Paths in a Grid | |
#only X,Y+1 and X+1,y movements allowed | |
# -1 is obstacle | |
class Solution: | |
def get_number_of_possible_paths(self, list_of_rows): | |
potencial_list = copy.copy(list_of_rows) | |
lo = list_of_rows | |
idx_len = len(lo) | |
jdx_len = len(lo[idx_len-1]) | |
for idx,line in enumerate(list_of_rows): | |
for jdx,obj in enumerate(line): | |
if(list_of_rows[idx][jdx] != -1): #not an obstacle | |
counter = 1 if (idx == 0 and jdx == 0) else 0 | |
if(jdx != 0 and list_of_rows[idx][jdx-1] != -1): | |
counter += lo[idx][jdx-1] | |
if(idx != 0 and list_of_rows[idx-1][jdx] != -1): | |
counter += lo[idx-1][jdx] | |
potencial_list[idx][jdx] = counter | |
return potencial_list[idx_len-1][jdx_len-1] | |
myc = Solution() | |
possible_paths = myc.get_number_of_possible_paths([[0,0,-1,0], | |
[0,0,-1,0], | |
[0,0,0,0]]) | |
print("There is {0} possible paths".format(possible_paths)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment