You are viewing a single comment's thread. Return to all comments →
O(n) time-complexity; O(1) space-complexity. Passes all cases.
public static int fibonacci(int n) { int[] fib = new int[2]; fib[0] = 0; fib[1] = 1; for (int i = 2; i <= n; ++i) { fib[i % 2] = fib[0] + fib[1]; } return fib[n % 2]; }
Seems like cookies are disabled on this browser, please enable them to open this website
Recursion: Fibonacci Numbers
You are viewing a single comment's thread. Return to all comments →
O(n) time-complexity; O(1) space-complexity. Passes all cases.