GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/dp/climbing_stairs.py at master · vspriya05/algorithms Expand file tree/ Copy pathclimbing_stairs.pyMore file actions 32 lines (24 loc) · 569 Bytes / Copy pathclimbing_stairs.pyFile metadata and controls32 lines (24 loc) · 569 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 """ You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. """ # O(n) space def climb_stairs(n): """ :type n: int :rtype: int """ arr = [1, 1] for i in range(2, n+1): arr.append(arr[-1] + arr[-2]) return arr[-1] # the above function can be optimized as: # O(1) space def climb_stairs_optimized(n): a = b = 1 for _ in range(n): a, b = b, a + b return a |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |