diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/Paint_House.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/Paint_House.py new file mode 100644 index 00000000..e22f4e77 --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/Paint_House.py @@ -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 diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/SubSet_Sum.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/SubSet_Sum.py new file mode 100644 index 00000000..52632f54 --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/SubSet_Sum.py @@ -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 \ No newline at end of file diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/nth_tribonacci_num.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/nth_tribonacci_num.py new file mode 100644 index 00000000..2725aee1 --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/nth_tribonacci_num.py @@ -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 diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/zero_one_knapsack.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/zero_one_knapsack.py new file mode 100644 index 00000000..b998364c --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/zero_one_knapsack.py @@ -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 diff --git a/Project-Structure.md b/Project-Structure.md index 1409ef1e..1e1c75f9 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -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)