Skip to content

Commit 8ad6408

Browse files
Some Dynamic Programming Concept
1 parent b26f436 commit 8ad6408

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "4a8d9a75",
6+
"metadata": {},
7+
"source": [
8+
"# Climibing Staris "
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "6c331f02",
14+
"metadata": {},
15+
"source": [
16+
"### Recursion"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "09992779",
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"name": "stdout",
27+
"output_type": "stream",
28+
"text": [
29+
"13\n"
30+
]
31+
}
32+
],
33+
"source": [
34+
"n = 6\n",
35+
"if n<2:\n",
36+
" print(1)\n",
37+
"\n",
38+
"a, b = 1, 1\n",
39+
"for _ in range(2, n+1):\n",
40+
" a, b = b, a+b\n",
41+
"\n",
42+
"print(b)"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"id": "e5bc268f",
48+
"metadata": {},
49+
"source": [
50+
"### Dynamic Approach"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 8,
56+
"id": "71177526",
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"data": {
61+
"text/plain": [
62+
"8"
63+
]
64+
},
65+
"execution_count": 8,
66+
"metadata": {},
67+
"output_type": "execute_result"
68+
}
69+
],
70+
"source": [
71+
"n = 5\n",
72+
"dp = [0]*(n+1)\n",
73+
"dp[0] = 1\n",
74+
"dp[1] = 1\n",
75+
"\n",
76+
"for i in range(2, n+1):\n",
77+
" dp[i] = dp[i-1]+dp[i-2]\n",
78+
"\n",
79+
"dp[n]"
80+
]
81+
}
82+
],
83+
"metadata": {
84+
"kernelspec": {
85+
"display_name": "Python 3",
86+
"language": "python",
87+
"name": "python3"
88+
},
89+
"language_info": {
90+
"codemirror_mode": {
91+
"name": "ipython",
92+
"version": 3
93+
},
94+
"file_extension": ".py",
95+
"mimetype": "text/x-python",
96+
"name": "python",
97+
"nbconvert_exporter": "python",
98+
"pygments_lexer": "ipython3",
99+
"version": "3.13.3"
100+
}
101+
},
102+
"nbformat": 4,
103+
"nbformat_minor": 5
104+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f8990bae",
6+
"metadata": {},
7+
"source": [
8+
"# N-th Triobnacci Number"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 2,
14+
"id": "edee84b6",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"data": {
19+
"text/plain": [
20+
"13"
21+
]
22+
},
23+
"execution_count": 2,
24+
"metadata": {},
25+
"output_type": "execute_result"
26+
}
27+
],
28+
"source": [
29+
"n=6\n",
30+
"\n",
31+
"if n==0 or n==1:\n",
32+
" print(n)\n",
33+
"dp = [0]*(n+1)\n",
34+
"dp[0], dp[1], dp[2] = 0, 1, 1\n",
35+
"\n",
36+
"for i in range(3, n+1):\n",
37+
" dp[i] = dp[i-1] + dp[i-2] + dp[i-3]\n",
38+
"\n",
39+
"dp[n]"
40+
]
41+
}
42+
],
43+
"metadata": {
44+
"kernelspec": {
45+
"display_name": "Python 3",
46+
"language": "python",
47+
"name": "python3"
48+
},
49+
"language_info": {
50+
"codemirror_mode": {
51+
"name": "ipython",
52+
"version": 3
53+
},
54+
"file_extension": ".py",
55+
"mimetype": "text/x-python",
56+
"name": "python",
57+
"nbconvert_exporter": "python",
58+
"pygments_lexer": "ipython3",
59+
"version": "3.13.3"
60+
}
61+
},
62+
"nbformat": 4,
63+
"nbformat_minor": 5
64+
}

0 commit comments

Comments
 (0)