Skip to content

Commit 41e41d8

Browse files
authored
Update dynamic-programming.md
1 parent 19ab613 commit 41e41d8

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

contrib/ds-algorithms/dynamic-programming.md

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ print(f"The {n}th Fibonacci number is: {fibonacci(n)}.")
5151
- **Time Complexity**: O(n) for both approaches
5252
- **Space Complexity**: O(n) for the top-down approach (due to memoization), O(1) for the bottom-up approach
5353

54-
</br>
55-
<hr>
56-
</br>
57-
5854
# 2. Longest Common Subsequence
5955

6056
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))
112108
- **Time Complexity**: O(m * n) for both approaches, where m and n are the lengths of the input sequences
113109
- **Space Complexity**: O(m * n) for the memoization table
114110

115-
</br>
116-
<hr>
117-
</br>
118-
119111
# 3. 0-1 Knapsack Problem
120112

121113
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
146138
n = len(weights)
147139
print("Maximum value that can be obtained:", knapsack(weights, values, capacity, n))
148140
```
141+
149142
## 0-1 Knapsack Problem Code in Python (Bottom-up Approach)
150143

151144
```python
@@ -170,14 +163,11 @@ capacity = 50
170163
n = len(weights)
171164
print(knapSack(capacity, weights, values, n))
172165
```
166+
173167
## Complexity Analysis
174168
- **Time Complexity**: O(n * W) for both approaches, where n is the number of items and W is the capacity of the knapsack
175169
- **Space Complexity**: O(n * W) for the memoization table
176170

177-
</br>
178-
<hr>
179-
</br>
180-
181171
# 4. Longest Increasing Subsequence
182172

183173
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
190180
## Longest Increasing Subsequence Code in Python (Top-Down Approach using Memoization)
191181

192182
```python
193-
194183
import sys
195184

196185
def f(idx, prev_idx, n, a, dp):
@@ -241,10 +230,7 @@ def lis(arr):
241230
arr = [10, 22, 9, 33, 21, 50, 41, 60]
242231
print("Length of lis is", lis(arr))
243232
```
233+
244234
## Complexity Analysis
245235
- **Time Complexity**: O(n * n) for both approaches, where n is the length of the array.
246236
- **Space Complexity**: O(n * n) for the memoization table in Top-Down Approach, O(n) in Bottom-Up Approach.
247-
248-
</br>
249-
<hr>
250-
</br>

0 commit comments

Comments
 (0)