Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more Problems in Dynamic programming #857

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Problem: You are tasked with painting houses. Each house can be painted in one of k colors,
and no two adjacent houses can have the same color. Find the minimum cost to paint all houses.
"""
def paint_house(costs):
if not costs:
return 0

n = len(costs)
k = len(costs[0])
dp = costs[0][:]

for i in range(1, n):
prev_dp = dp[:]
for j in range(k):
dp[j] = costs[i][j] + min(prev_dp[m] for m in range(k) if m != j)

return min(dp)

# Example usage
costs = [[17, 2, 17], [16, 16, 5], [14, 3, 19]]
print(f"Minimum cost to paint all houses: {paint_house(costs)}") # Output: Minimum cost to paint all houses: 10
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Problem: Given a set of integers, find if there is a subset with sum equal to a given number.


def subset_sum(nums, target):
dp = [False] * (target + 1)
dp[0] = True

for num in nums:
for i in range(target, num - 1, -1):
dp[i] = dp[i] or dp[i - num]

return dp[target]

# Example usage
nums = [3, 34, 4, 12, 5, 2]
target = 9
print(f"Is there a subset with sum {target}? {'Yes' if subset_sum(nums, target) else 'No'}") # Output: Yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Problem: Similar to the Fibonacci sequence, the Tribonacci sequence is defined as:
dp[n] = dp[n-1] + dp[n-2] + dp[n-3].
Given n, find the N-th Tribonacci number.
"""

def tribonacci(n):
if n == 0:
return 0
elif n == 1 or n == 2:
return 1

dp = [0] * (n + 1)
dp[0], dp[1], dp[2] = 0, 1, 1

for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]

return dp[n]

# Example usage
n = 10
print(f"The {n}-th Tribonacci number is: {tribonacci(n)}") # Output: The 10-th Tribonacci number is: 149
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 0/1 Knapsack Problem Solution

# Problem: Given n items with weight and value, find the maximum value you can carry in a knapsack of capacity W.

def knapsack(weights, values, W):
n = len(weights)
# Create a 2D array to store maximum values
dp = [[0] * (W + 1) for _ in range(n + 1)]

# Fill the dp array
for i in range(1, n + 1):
for w in range(W + 1):
if weights[i - 1] <= w:
# Maximize value for the current item
dp[i][w] = max(dp[i - 1][w], values[i - 1] + dp[i - 1][w - weights[i - 1]])
else:
dp[i][w] = dp[i - 1][w]

return dp[n][W]

# Example usage
weights = [1, 2, 3]
values = [10, 20, 30]
W = 5
print(f"Maximum value in Knapsack: {knapsack(weights, values, W)}") # Output: Maximum value in Knapsack: 50
4 changes: 4 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@
* [Test Main](Algorithms_and_Data_Structures/Dijkstra/test_main.py)
* Dynamic-Programming-Series
* Basic-Dp-Problems
* [Paint House](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/Paint_House.py)
* [Subset Sum](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/SubSet_Sum.py)
* [Climbing-Stairs](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/climbing-stairs.py)
* [Fibonacci-Seq](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/fibonacci-seq.py)
* [House-Robber](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/house-robber.py)
* [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)
* Medium-Dp-Problems
* [Coin-Change](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/coin-change.py)
* [Longest-Inc-Subseq](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Medium-DP-Problems/longest-inc-subseq.py)
Expand Down