We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c2eec66 commit dbd3dbdCopy full SHA for dbd3dbd
src/_Classics_/fibonacci/index.js
@@ -8,3 +8,29 @@ function fibonacci(position) {
8
// element at position(p) (p -1) and (p - 2)
9
return fibonacci(position - 2) + fibonacci(position - 1);
10
}
11
+
12
+/**
13
+ * Memoization. In computing, memoization or memoisation is an
14
+ * optimization technique used primarily to speed up computer
15
+ * programs by storing the results of expensive function
16
+ * calls and returning the cached result when the
17
+ * same inputs occur again
18
+ */
19
20
+// Linear time, test with index as 510 for both the functions
21
+function fibonacciMemoized(index, cache) {
22
+ cache = cache || [];
23
24
+ if (cache[index]) {
25
+ return cache[index];
26
+ } else {
27
+ if (index < 3) {
28
+ return 1;
29
30
+ cache[index] =
31
+ fibonacciMemoized(index - 1, cache) +
32
+ fibonacciMemoized(index - 2, cache);
33
+ }
34
35
36
+}
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments