Skip to content

Commit a50198c

Browse files
authored
Merge pull request #238 from chvmvd/revise-fib-dp-code
2 parents 5fffaa3 + 7eb8cca commit a50198c

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

static/dp/fib_bottom_up.ipynb

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
{
2+
"nbformat": 4,
3+
"nbformat_minor": 2,
4+
"metadata": {},
25
"cells": [
36
{
4-
"cell_type": "code",
5-
"execution_count": null,
67
"metadata": {},
7-
"outputs": [],
88
"source": [
9-
"dp = [0 for _ in range(10000000)]\n",
10-
"\n",
11-
"\n",
129
"def fib(n):\n",
13-
" if n == 0:\n",
14-
" return dp[0]\n",
15-
" if n == 1:\n",
16-
" return dp[1]\n",
17-
" dp[0] = 0\n",
18-
" dp[1] = 1\n",
10+
" dp = [0, 1]\n",
11+
" if n == 0 or n == 1:\n",
12+
" return dp[n]\n",
1913
" for i in range(2, n + 1):\n",
20-
" dp[i] = dp[i - 1] + dp[i - 2]\n",
14+
" dp.append(dp[i - 1] + dp[i - 2])\n",
2115
" return dp[n]\n",
2216
"\n",
2317
"\n",
2418
"print(fib(100))"
25-
]
19+
],
20+
"cell_type": "code",
21+
"outputs": [],
22+
"execution_count": null
2623
}
27-
],
28-
"metadata": {},
29-
"nbformat": 4,
30-
"nbformat_minor": 2
24+
]
3125
}

0 commit comments

Comments
 (0)