-
Notifications
You must be signed in to change notification settings - Fork 214
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 #857 from siri-chandana-macha/new
Added more Problems in Dynamic programming
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/Paint_House.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 @@ | ||
""" | ||
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 |
17 changes: 17 additions & 0 deletions
17
Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/SubSet_Sum.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,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 |
23 changes: 23 additions & 0 deletions
23
...ms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/nth_tribonacci_num.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,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 |
25 changes: 25 additions & 0 deletions
25
...hms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/zero_one_knapsack.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,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 |
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