Last active
December 19, 2015 00:49
-
-
Save leeacto/5870968 to your computer and use it in GitHub Desktop.
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
| def paths(row, col) | |
| return 1 if row == 3 && col == 3 | |
| return 0 if row > 3 || col > 3 | |
| return paths(row + 1, col) + paths(row, col + 1) | |
| end | |
| def new_path(tot_row, tot_col) | |
| curr_row = 0 | |
| curr_col = 0 | |
| known = {} | |
| #Get all values in each row | |
| until curr_col > tot_row do | |
| #Get all values in column | |
| until curr_row > tot_row do | |
| if curr_col == 0 | |
| known[[curr_row,curr_col]] = 1 | |
| elsif curr_row == curr_col | |
| val = known[[curr_row,curr_col - 1]] * 2 | |
| known[[curr_row,curr_col]] = val | |
| else | |
| val = known[[curr_row,curr_col - 1]] + known[[curr_row - 1,curr_col]] | |
| known[[curr_row,curr_col]] = val | |
| end | |
| curr_row += 1 | |
| end | |
| curr_col += 1 | |
| curr_row = curr_col | |
| end | |
| known[[tot_row, tot_col]] | |
| end | |
| st = Time.new | |
| puts new_path(20,20).inspect | |
| en = Time.new - st | |
| puts en |
jfarmer
commented
Jun 26, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment