Advertisement
ogv

Untitled

ogv
Aug 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. /*
  2. Runtime: 0 ms, faster than 100.00% of Java online submissions for Climbing Stairs.
  3. Memory Usage: 33 MB, less than 5.26% of Java online submissions for Climbing Stairs.
  4. */
  5. class Solution {
  6.     public int climbStairs(int n) {
  7.         if (n < 0) return 0;
  8.        
  9.         int[] cache = new int[n + 1];
  10.         return climb(n, cache);
  11.     }
  12.    
  13.     private int climb(int n, int[] cache) {        
  14.         if (n <= 1) return 1;
  15.        
  16.         if (cache[n] > 0) {
  17.             return cache[n];
  18.         }
  19.        
  20.         int result = climb(n - 2, cache) + climb(n - 1, cache);
  21.         cache[n] = result;
  22.         return result;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement