You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contrib/ds-algorithms/dynamic-programming.md
+3-17Lines changed: 3 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,10 +51,6 @@ print(f"The {n}th Fibonacci number is: {fibonacci(n)}.")
51
51
-**Time Complexity**: O(n) for both approaches
52
52
-**Space Complexity**: O(n) for the top-down approach (due to memoization), O(1) for the bottom-up approach
53
53
54
-
</br>
55
-
<hr>
56
-
</br>
57
-
58
54
# 2. Longest Common Subsequence
59
55
60
56
The longest common subsequence (LCS) problem is to find the longest subsequence common to two sequences. A subsequence is a sequence that appears in the same relative order but not necessarily contiguous.
@@ -112,10 +108,6 @@ print("Length of LCS is", longestCommonSubsequence(S1, S2, m, n))
112
108
-**Time Complexity**: O(m * n) for both approaches, where m and n are the lengths of the input sequences
113
109
-**Space Complexity**: O(m * n) for the memoization table
114
110
115
-
</br>
116
-
<hr>
117
-
</br>
118
-
119
111
# 3. 0-1 Knapsack Problem
120
112
121
113
The 0-1 knapsack problem is a classic optimization problem where the goal is to maximize the total value of items selected while keeping the total weight within a specified limit.
@@ -146,6 +138,7 @@ capacity = 50
146
138
n =len(weights)
147
139
print("Maximum value that can be obtained:", knapsack(weights, values, capacity, n))
148
140
```
141
+
149
142
## 0-1 Knapsack Problem Code in Python (Bottom-up Approach)
150
143
151
144
```python
@@ -170,14 +163,11 @@ capacity = 50
170
163
n =len(weights)
171
164
print(knapSack(capacity, weights, values, n))
172
165
```
166
+
173
167
## Complexity Analysis
174
168
-**Time Complexity**: O(n * W) for both approaches, where n is the number of items and W is the capacity of the knapsack
175
169
-**Space Complexity**: O(n * W) for the memoization table
176
170
177
-
</br>
178
-
<hr>
179
-
</br>
180
-
181
171
# 4. Longest Increasing Subsequence
182
172
183
173
The Longest Increasing Subsequence (LIS) is a task is to find the longest subsequence that is strictly increasing, meaning each element in the subsequence is greater than the one before it. This subsequence must maintain the order of elements as they appear in the original sequence but does not need to be contiguous. The goal is to identify the subsequence with the maximum possible length.
@@ -190,7 +180,6 @@ The Longest Increasing Subsequence (LIS) is a task is to find the longest subseq
190
180
## Longest Increasing Subsequence Code in Python (Top-Down Approach using Memoization)
191
181
192
182
```python
193
-
194
183
import sys
195
184
196
185
deff(idx, prev_idx, n, a, dp):
@@ -241,10 +230,7 @@ def lis(arr):
241
230
arr = [10, 22, 9, 33, 21, 50, 41, 60]
242
231
print("Length of lis is", lis(arr))
243
232
```
233
+
244
234
## Complexity Analysis
245
235
-**Time Complexity**: O(n * n) for both approaches, where n is the length of the array.
246
236
-**Space Complexity**: O(n * n) for the memoization table in Top-Down Approach, O(n) in Bottom-Up Approach.
0 commit comments