Created
October 26, 2023 02:26
-
-
Save themichaelyang/7316a45b356aa34bd2b44641443aff7c to your computer and use it in GitHub Desktop.
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
def unique_paths(m, n) | |
grid = Array.new(m) | |
m.times.with_index do |y| | |
grid[y] = Array.new(n) | |
grid[y][0] = 1 | |
n.times.with_index do |x| | |
from_left = if x - 1 >= 0 then grid[y][x - 1] else 0 end | |
from_above = if y - 1 >= 0 then grid[y - 1][x] else 0 end | |
grid[y][x] ||= from_left + from_above | |
end | |
end | |
grid.last.last | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment