Advertisement
LeatherDeer

Untitled

Oct 20th, 2022
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. class Solution {
  2.     public int climbStairs(int n) {
  3.         int[] cache = new int[n + 1];
  4.         cache[0] = 1;
  5.         cache[1] = 1;
  6.        
  7.         helper(n, cache);
  8.         return cache[n];
  9.     }
  10.  
  11.     private int helper(int n, int[] cache) {
  12.         if (cache[n] != 0) {
  13.             return cache[n];
  14.         }
  15.  
  16.         cache[n] = helper(n - 1, cache) + helper(n - 2, cache);
  17.         return cache[n];
  18.  
  19.     }
  20. }
  21.  
  22. class Solution {
  23.     List<List<Integer>> ans; // переменная класса
  24.    
  25.     public List<List<Integer>> combine(int n, int k) {
  26.         ans = new ArrayList<>();
  27.         helper(0, new ArrayList<>(), n, k);
  28.         return ans;
  29.     }
  30.    
  31.     private void helper(int prev, List<Integer> comb, int n, int k) {
  32.         if (comb.size() == k) {
  33.             ans.add(new ArrayList<>(comb));
  34.             return;
  35.         }
  36.        
  37.         for (int i = prev + 1; i <= n; i++) {
  38.             comb.add(i);
  39.             helper(i, comb, n, k);
  40.             comb.remove(comb.size() - 1);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement