forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request UTSAVS26#1094 from NK-Works/dp-3
Catalan Algo, Min Steps to One and Knapsack With Constraints Added
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...rithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/catalan-numbers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def catalan_numbers(n): | ||
# Initialize a list to store Catalan numbers | ||
catalan = [0] * (n + 1) | ||
|
||
# Base case | ||
catalan[0] = 1 | ||
|
||
# Calculate the remaining catalan numbers up to nth | ||
for i in range(1, n + 1): | ||
catalan[i] = sum(catalan[j] * catalan[i - 1 - j] for j in range(i)) | ||
|
||
return catalan | ||
|
||
# Test the function | ||
n = 10 # Calculate the first 10 Catalan numbers | ||
print(f"The first {n} Catalan numbers are: {catalan_numbers(n)}") |
22 changes: 22 additions & 0 deletions
22
..._Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/knapsack-with-constraints.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
def knapsack(weights, values, capacity): | ||
n = len(weights) | ||
# Create a DP array with (n+1) rows for items and (capacity+1) columns for capacity | ||
dp = [[0] * (capacity + 1) for _ in range(n + 1)] | ||
|
||
# Fill the DP table | ||
for i in range(1, n + 1): | ||
for w in range(1, capacity + 1): | ||
if weights[i - 1] <= w: | ||
# Include the item or exclude it, choose the better option | ||
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]) | ||
else: | ||
# Cannot include the item, so carry forward the value without it | ||
dp[i][w] = dp[i - 1][w] | ||
|
||
return dp[n][capacity] | ||
|
||
# Test the function | ||
weights = [1, 2, 3, 5] | ||
values = [10, 20, 30, 50] | ||
capacity = 6 | ||
print(f"Maximum value in the knapsack: {knapsack(weights, values, capacity)}") |
21 changes: 21 additions & 0 deletions
21
...ithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/min-steps-to-one.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def min_steps_to_one(n): | ||
# Initialize a DP array where dp[i] will store minimum steps to reach 1 from i | ||
dp = [float('inf')] * (n + 1) | ||
dp[1] = 0 # Base case: it takes 0 steps to reach 1 from 1 | ||
|
||
# Fill the DP table for each number from 2 up to n | ||
for i in range(2, n + 1): | ||
# Subtract 1 | ||
dp[i] = dp[i - 1] + 1 | ||
# Divide by 2 if applicable | ||
if i % 2 == 0: | ||
dp[i] = min(dp[i], dp[i // 2] + 1) | ||
# Divide by 3 if applicable | ||
if i % 3 == 0: | ||
dp[i] = min(dp[i], dp[i // 3] + 1) | ||
|
||
return dp[n] | ||
|
||
# Test the function | ||
n = 10 | ||
print(f"Minimum steps to reduce {n} to 1: {min_steps_to_one(n)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters