Skip to content

Instantly share code, notes, and snippets.

@themichaelyang
Created October 26, 2023 02:26
Show Gist options
  • Save themichaelyang/7316a45b356aa34bd2b44641443aff7c to your computer and use it in GitHub Desktop.
Save themichaelyang/7316a45b356aa34bd2b44641443aff7c to your computer and use it in GitHub Desktop.
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