You are viewing a single comment's thread. Return to all comments →
You only need to remember the last 3 values so just store them in an int[] and update until you are done - C# code:
static int steps(int n) { int[] arr = {1,2,4}; if (n < 4) return arr[n - 1]; for (int i = 5; i <= n; i++){ arr[(i + 1) % 3] = arr[0] + arr[1] + arr[2]; } return arr[0] + arr[1] + arr[2]; }
Recursion: Davis' Staircase
You are viewing a single comment's thread. Return to all comments →
You only need to remember the last 3 values so just store them in an int[] and update until you are done - C# code: