Skip to content

Commit

Permalink
Merge pull request #857 from siri-chandana-macha/new
Browse files Browse the repository at this point in the history
Added more Problems in Dynamic programming
  • Loading branch information
UTSAVS26 authored Nov 5, 2024
2 parents 92b2724 + d4c2a16 commit d981f68
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
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 @@ -85,11 +85,15 @@
* [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)
* Best-Time-To-Buy-And-Sell-Stock
* [Program](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/best-time-to-buy-and-sell-stock/program.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)
* Hard-Dp-Problems
* [Cherry-Pick-Algo](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/cherry-pick-algo.py)
* [Levenshtein-Distance](Algorithms_and_Data_Structures/Dynamic-Programming-Series/Hard-DP-Problems/levenshtein-distance.py)
Expand Down

0 comments on commit d981f68

Please sign in to comment.