From c4a91a855fa12025f1d8a9af2d46abda14609369 Mon Sep 17 00:00:00 2001 From: sabyasachi bisoyi Date: Sun, 4 Jan 2026 00:13:34 -0800 Subject: [PATCH] DP 2 --- CoinChange2.java | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ PaintHouse.java | 25 +++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 CoinChange2.java create mode 100644 PaintHouse.java diff --git a/CoinChange2.java b/CoinChange2.java new file mode 100644 index 00000000..8f635596 --- /dev/null +++ b/CoinChange2.java @@ -0,0 +1,94 @@ +// Time Complexity : O(2pow(amount+(amount/smallest coin)) +// Space Complexity : O(2pow(amount+(amount/smallest coin)) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : TLE + + +// Your code here along with comments explaining your approach +// We follow the before approach either to take or not take the current coin in recursion +class Solution { + public int change(int amount, int[] coins) { + int[][] mem = new int[coins.length][amount+1]; + return helper(amount,coins,0,mem); + } + + private int helper(int amount, int[] coins, int index, int[][] mem) + { + //base case + if(amount==0) + { + return 1; + } + if(amount<0 || index>=coins.length) + { + return 0; + } + if(mem[index][amount]){} + //if current coin is taken + int case1 = helper(amount-coins[index],coins,index); + //if current coin is not taken + int case2 = helper(amount,coins,index+1); + return case1+case2; + } +} + +// Time Complexity : O(coins len * amount) +// Space Complexity : O(coins len * amount) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : TLE + + +// Your code here along with comments explaining your approach +// We follow the before approach either to take or not take the current coin in recursion with memorization +// still TLE +class Solution { + public int change(int amount, int[] coins) { + int[][] mem = new int[coins.length][amount+1]; + return helper(amount,coins,0,mem); + } + + private int helper(int amount, int[] coins, int index,int[][] mem) + { + //base case + if(amount==0) + { + return 1; + } + if(amount<0 || index>=coins.length) + { + return 0; + } + //use memorized solution if exists + if(mem[index][amount]!=0) + { + return mem[index][amount]; + } + //if current coin is taken + int case1 = helper(amount-coins[index],coins,index,mem); + //if current coin is not taken + int case2 = helper(amount,coins,index+1,mem); + int pathPossible = case1+case2; + //put to memorized solution + mem[index][amount] = pathPossible; + return pathPossible; + } +} + +// Time Complexity : O( amount) +// Space Complexity : O(amount) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Yes +class Solution { + public int change(int amount, int[] coins) { + int[] mem = new int[amount+1]; + mem[0] = 1; + for(int coin : coins) + { + for(int i = coin;i<=amount;i++) + { + mem[i]+=mem[i-coin]; + } + } + return mem[amount]; + } +} \ No newline at end of file diff --git a/PaintHouse.java b/PaintHouse.java new file mode 100644 index 00000000..ae4785a7 --- /dev/null +++ b/PaintHouse.java @@ -0,0 +1,25 @@ +// Time Complexity : O(costs len) +// Space Complexity : O(costs len) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Yes + + +// Your code here along with comments explaining your approach +// We start bottom up, calculate at each step what's the min possible by taking prev sides sum +class Solution { + + public int minCost(int[][] costs) { + //edge case + if (costs.length == 0) return 0; + + int[][] memo = new int[costs.length + 1][3]; + //bottom-up, taking the min sum from prev two + for (int i = costs.length - 1; i >= 0; --i) { + memo[i][0] = costs[i][0] + Math.min(memo[i + 1][1], memo[i + 1][2]); + memo[i][1] = costs[i][1] + Math.min(memo[i + 1][0], memo[i + 1][2]); + memo[i][2] = costs[i][2] + Math.min(memo[i + 1][0], memo[i + 1][1]); + } + //return min of top + return Math.min(Math.min(memo[0][0], memo[0][1]), memo[0][2]); + } +} \ No newline at end of file