From eba922d882a96a02f4da753b4baaa380b7c55e63 Mon Sep 17 00:00:00 2001 From: Sahithipsl470 Date: Sat, 22 Nov 2025 14:57:33 -0500 Subject: [PATCH] "DP-2 done" --- Problem-1.java | 31 +++++++++++++++++++++++++++++++ Problem-1.py | 24 ++++++++++++++++++++++++ Problem-2.java | 27 +++++++++++++++++++++++++++ Problem-2.py | 21 +++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 Problem-1.java create mode 100644 Problem-1.py create mode 100644 Problem-2.java create mode 100644 Problem-2.py diff --git a/Problem-1.java b/Problem-1.java new file mode 100644 index 00000000..1158e5ce --- /dev/null +++ b/Problem-1.java @@ -0,0 +1,31 @@ +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :No +// Any problem you faced while coding this :No + + +// Your code here along with comments explaining your approach +//Keep a DP array of size 3 to store the minimum cost to paint the previous house in each color. +// For each house, update each color’s cost by adding its paint cost to the minimum of the other two colors from the previous house. +// After processing all houses, return the minimum value among the three colors, which gives the overall minimum painting cost. + +class Solution { + public int minCost(int[][] costs) { + int n = costs.length; + if( n==0){ + return 0; + } + int[] dp = new int[3]; + dp[0] = costs[0][0]; + dp[1] = costs[0][1]; + dp[2] = costs[0][2]; + + for( int i =1; i < n; i++){ + int temp = dp[0]; + dp[0] = costs[i][0] + Math.min(dp[1],dp[2]); + int temp_dp1 = dp[1]; + dp[1] = costs[i][1] + Math.min(temp,dp[2]); + dp[2] = costs[i][2]+ Math.min(temp,temp_dp1); + } + return Math.min(dp[0], Math.min(dp[1],dp[2])); + }} \ No newline at end of file diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..1f7a707e --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,24 @@ +# Time Complexity :O(n) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode :No +# Any problem you faced while coding this :No + + +# Your code here along with comments explaining your approach +#Keep a DP array of size 3 to store the minimum cost to paint the previous house in each color. +# For each house, update each color’s cost by adding its paint cost to the minimum of the other two colors from the previous house. +# After processing all houses, return the minimum value among the three colors, which gives the overall minimum painting cost. + +class Solution: + def minCost(self, costs): + n = len(costs) + m = len(costs[0]) + dp = costs[0][:] + if n ==0: + return 0 + for i in range(1,n): + prev_dp = dp[:] + dp[0] = costs[i][0] + min(prev_dp[1], prev_dp[2]) + dp[1] = costs[i][1] + min(prev_dp[0], prev_dp[2]) + dp[2] = costs[i][2] + min(prev_dp[0], prev_dp[1]) + return min(dp[0],dp[1],dp[2]) \ No newline at end of file diff --git a/Problem-2.java b/Problem-2.java new file mode 100644 index 00000000..9613e6e0 --- /dev/null +++ b/Problem-2.java @@ -0,0 +1,27 @@ +// Time Complexity :O(mn) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No + + +// Your code here along with comments explaining your approach +//I used a DP array where dp[i] stores the number of ways to make amount i. +// For each coin, I iterate through all amounts ≥ coin value and update dp[j] += dp[j - coin] to add combinations including this coin. +// At the end, dp[amount] contains the total number of combinations to form the target amount. + +class Solution { + public int change(int amount, int[] coins) { + int n = coins.length; + int[] dp = new int[amount+1]; + dp[0] = 1; + if (amount <0 || n == 0){ + return 0; + } + for (int i = 0; i < n; i++){ + for (int j = coins[i]; j < amount+1; j++){ + dp[j] = dp[j] + dp[j-coins[i]]; + } + } + return dp[amount]; + } +} \ No newline at end of file diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..ff72a54d --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,21 @@ +# Time Complexity :O(mn) +# Space Complexity :O(n) +# Did this code successfully run on Leetcode :Yes +# Any problem you faced while coding this :No + + +# Your code here along with comments explaining your approach +#I used a DP array where dp[i] stores the number of ways to make amount i. +# For each coin, I iterate through all amounts ≥ coin value and update dp[j] += dp[j - coin] to add combinations including this coin. +# At the end, dp[amount] contains the total number of combinations to form the target amount. + +class Solution: + def change(self, amount: int, coins: List[int]) -> int: + dp = [0]* (amount+1) + dp[0] = 1 + if len(coins) == 0 or amount < 0: + return 0 + for coin in coins: + for j in range(coin,amount+1): + dp[j] = dp[j]+ dp[j-coin] + return dp[amount] \ No newline at end of file