LeetCode in Ruby (11 Part Series)
def climb_stairs(n) p = 1 q = 1 (n - 1).times do temp = q q += p p = temp end q end
This problem can be solved by using dynamic programming. We define
f(n) as the number of ways you climb to the
nth step from:
- one step from
n-1th step or,
- two steps from
f(n) = f(n-1) + f(n-2), which is the same as the Fibonacci sequence. We have two bases cases:
f(1) = 1 and
f(2) = 2. The lines 4 and 5 are the first two numbers of the Fibonacci sequence. To get the nth number, we add up the numbers up to nth number. Here, we optimize the performance the just storing the previous two numbers.
There are a lot of people who love both JS and UX/CSS. If we stop labeling people just as “JS developers” or “UX developers”, we can achieve a ceasefire in the current “JS vs. CSS” war and achieve a mutually benefiting peace.