Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<Integer> list = new ArrayList<>();
- public static int s(int n){
- while (list.size() <= n) {
- list.add(-1); // initialize with a dummy value
- }
- if(n<=2){
- list.set(n,1);
- return 1;
- }
- if(list.get(n) != -1){
- return list.get(n);
- }
- int result = s(n-1)+s(n-2);
- list.set(n,result);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement