You are viewing a single comment's thread. Return to all comments →
Java DP solution, no recursion needed. Runtime O(n)
public static int calcNum(int n) { int[] array = new int[n]; if (n == 1) { return 1; } else if(n == 2) { return 2; } else if(n == 3) { return 4; } array[0] = 1; array[1] = 2; array[2] = 4; for (int i = 3; i < n; i++) { array[i] = array[i-1] + array[i-2] + array[i-3]; } return array[array.length-1]; }
Seems like cookies are disabled on this browser, please enable them to open this website
Recursion: Davis' Staircase
You are viewing a single comment's thread. Return to all comments →
Java DP solution, no recursion needed. Runtime O(n)