Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class NSteps123 {
- public static void main (String[] args) {
- int input = 3;
- int[] cache = new int[input+1];
- Arrays.fill(cache,-1);
- countSteps(cache,input);
- System.out.println(countSteps(cache, input));
- }
- public static int countSteps(int[] cache, int num) {
- if(num < 0){
- return 0;
- }
- if(num == 0 || num == 1){
- return 1;
- }
- if(num == 2){
- return 2;
- }else{
- if(cache[num] > -1){
- return cache[num];
- }else{
- cache[num] = countSteps(cache, num-1) + countSteps(cache,num-2)+ countSteps(cache,num-3);
- }
- }
- return cache[num];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment