Skip to content

Commit

Permalink
Merge pull request UTSAVS26#1094 from NK-Works/dp-3
Browse files Browse the repository at this point in the history
Catalan Algo, Min Steps to One and Knapsack With Constraints Added
  • Loading branch information
UTSAVS26 authored Nov 5, 2024
2 parents 7c2b34b + 62f1d7d commit 1444b70
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
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)}")
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)}")
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)}")
3 changes: 3 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@
* [Nth Tribonacci Num](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/nth_tribonacci_num.py)
* [Zero One Knapsack](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/zero_one_knapsack.py)
* Hard-Dp-Problems
* [Catalan-Numbers](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/catalan-numbers.py)
* [Cherry-Pick-Algo](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/cherry-pick-algo.py)
* [Knapsack-With-Constraints](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/knapsack-with-constraints.py)
* [Levenshtein-Distance](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/levenshtein-distance.py)
* [Min-Steps-To-One](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/min-steps-to-one.py)
* [Regular-Expression-Matching](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/regular-expression-matching.py)
* Medium-Dp-Problems
* [Coin-Change](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/coin-change.py)
Expand Down

0 comments on commit 1444b70

Please sign in to comment.