File tree 1 file changed +31
-29
lines changed
1 file changed +31
-29
lines changed Original file line number Diff line number Diff line change @@ -23,35 +23,37 @@ why it helps?
23
23
24
24
top-down vs bottom up
25
25
26
- - top down (tabulation)
27
- - ** iteration** and starts with a basecase
28
- - order matters
29
- - usually faster, no recursion overhead
30
- ``` py
31
- # fibonacci
32
- F = [0 ] * (n+ 1 )
33
- F[0 ] = 1
34
- F[1 ] = 1
35
- for i in range (2 , n):
36
- F[i] = F[i- 1 ] + F[i- 2 ]
37
- ```
38
- - bottom- up (memoization)
39
-
40
- - ** recursion** and made efficient with memoization
41
- - easier to write, order does not matter
42
- - slower
43
-
44
- ```py
45
- memo = {}
46
- def f (i ):
47
- if i < 2 :
48
- return i
49
-
50
- if i not in memo:
51
- memo[i] = f(i- 1 ) + f(i- 2 )
52
-
53
- return memo[i]
54
- ```
26
+ top down (tabulation)
27
+
28
+ - ** iteration** and starts with a basecase
29
+ - order matters
30
+ - usually faster, no recursion overhead
31
+ ``` py
32
+ # fibonacci
33
+ F = [0 ] * (n+ 1 )
34
+ F[0 ] = 1
35
+ F[1 ] = 1
36
+ for i in range (2 , n):
37
+ F[i] = F[i- 1 ] + F[i- 2 ]
38
+ ```
39
+
40
+ bottom-up (memoization)
41
+
42
+ - ** recursion** and made efficient with memoization
43
+ - easier to write, order does not matter
44
+ - slower
45
+
46
+ ``` py
47
+ memo = {}
48
+ def f (i ):
49
+ if i < 2 :
50
+ return i
51
+
52
+ if i not in memo:
53
+ memo[i] = f(i- 1 ) + f(i- 2 )
54
+
55
+ return memo[i]
56
+ ```
55
57
56
58
when to use it?
57
59
You can’t perform that action at this time.
0 commit comments