From bfbaf0741baf8a420b33ba4d9967dcfba31948ff Mon Sep 17 00:00:00 2001 From: Joshua Yoon Date: Tue, 19 Oct 2021 19:24:07 -0500 Subject: [PATCH 1/3] Completed DP2 --- CoinChange2.java | 23 +++++++++++++++++++++++ PaintHouse.java | 38 ++++++++++++++++++++++++++++++++++++++ PaintHouse2.java | 16 ++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 CoinChange2.java create mode 100644 PaintHouse.java create mode 100644 PaintHouse2.java diff --git a/CoinChange2.java b/CoinChange2.java new file mode 100644 index 00000000..3172b8fe --- /dev/null +++ b/CoinChange2.java @@ -0,0 +1,23 @@ +public class CoinChange2 { + public int coin(int[] coins, int amount) { + int[][] dp = new int[coins.length + 1][amount + 1]; + + // initialize row + for (int i = 0; i <= coins.length; i++) { + dp[i][0] = 1; + } + for (int i = 0; i <= amount; i++) { + dp[0][i] = 0; + } + for (int row = 1; row <= coins.length; row++) { + for (int col = 1; col <= amount; col++) { + if (col < coins[row - 1]) { + dp[row][col] = dp[row - 1][col]; + } else { + dp[row][col] = dp[row - 1][col] + dp[row][col - coins[row - 1]]; + } + } + } + return dp[coins.length][amount]; + } +} diff --git a/PaintHouse.java b/PaintHouse.java new file mode 100644 index 00000000..cbb6f949 --- /dev/null +++ b/PaintHouse.java @@ -0,0 +1,38 @@ +class PaintHouse { + public int painthouse(int[][] costs){ + + //R G B + //0 1 2 + int red = helper(costs,0,0); + int green = helper(costs,0,1); + int blue = helper(costs,0,2); + + return Math.min(red, Math.min(green,blue)); + } + + private int helper(int[][] costs, int index, int lastRowsColor){ + //base condition + if (index==costs.length){ + return 0; + } + if (lastRowsColor==0){ + int blue = costs[index][0] + helper(costs, index+1, 2); + int green = costs[index][0] + helper( costs, index+1, 1); + + return Math.min(blue,green); + } + if (lastRowsColor ==1) { + int red = costs[index][1] + helper(costs, index+1,0); + int blue = costs[index][1] + helper(costs, index+1, 2); + + return Math.min(red,blue); + } + if (lastRowsColor ==2) { + int red = costs[index][2] + helper(costs, index+1,0); + int green = costs[index][2] + helper(costs, index+1, 1); + + return Math.min(red,green); + } + return -1; + } +} \ No newline at end of file diff --git a/PaintHouse2.java b/PaintHouse2.java new file mode 100644 index 00000000..0fa15d3b --- /dev/null +++ b/PaintHouse2.java @@ -0,0 +1,16 @@ +public class PaintHouse2 { + + public int paintHouse2(int[][] costs) { + // R G B + // 0 1 2 + for (int i = 1; i < costs.length; i++) { + // Red + costs[0][0] = costs[i][0] + Math.min(costs[i - 1][1], costs[i - 1][2]); + // Green + costs[0][1] = costs[i][1] + Math.min(costs[i - 1][0], costs[i - 1][2]); + // Blue + costs[0][2] = costs[i][2] + Math.min(costs[i - 1][0], costs[i - 1][1]); + } + return Math.min(costs[costs.length - 1][0], Math.min(costs[costs.length - 1][1], costs[costs.length - 1][2])); + } +} From f30bd8ed42121738fb5797a6517d1712f729a49f Mon Sep 17 00:00:00 2001 From: Joshua Yoon Date: Sat, 13 Jun 2026 22:04:41 -0500 Subject: [PATCH 2/3] dp-2 --- CoinChange2.java | 34 ++++++++++++++++++++ PaintHouse.java | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 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..9ba66a1a --- /dev/null +++ b/CoinChange2.java @@ -0,0 +1,34 @@ +public class CoinChange2 { + public int change(int amount, int[] coins) { + int n = coins.length; + int m = amount; + int[][] dp = new int[n+1][m+1]; + dp[0][0] = 1; + for (int i=1; i<=n; i++){ + for (int j=0; j<=m; j++){ + //till demom > amount -> 0 case + if (coins[i-1] > j) { + dp[i][j] = dp[i-1][j]; + } else { + dp[i][j] = dp[i-1][j] + dp[i][j-coins[i-1]]; + } + } + } + return dp[n][m]; + } + + // Time: O(2^(m+n)) + // public int change(int amount, int[] coins) { + // return helper(amount, coins, 0); + // } + // public int helper(int amount, int[] coins, int index){ + // //base + // if (amount < 0 || index == coins.length) return 0; + // if (amount == 0) return 1; + // //logic + // int case0 = helper(amount, coins, index+1); + // int case1 = helper(amount- coins[index], coins, index); + + // return case0 + case1; + // } +} diff --git a/PaintHouse.java b/PaintHouse.java new file mode 100644 index 00000000..80fe82c6 --- /dev/null +++ b/PaintHouse.java @@ -0,0 +1,81 @@ +public class PaintHouse { + //time: O(n) + public int minCost(int[][] costs) { + int n = costs.length; + // int[] dp = new int[3]; + int costR = costs[n-1][0]; + int costB = costs[n-1][1]; + int costG = costs[n-1][2]; + + for (int i= n-2; i>=0; i--){ + int tempR = costR; + costR = costs[i][0] + Math.min(costB,costG); + int tempB = costB; + costB = costs[i][1] + Math.min(tempR,costG); + costG = costs[i][2] + Math.min(tempB, tempR); + + + } + return Math.min(costR, Math.min(costB,costG)); + } + //time: O(n) + // public int minCost(int[][] costs) { + // int n = costs.length; + // int[] dp = new int[3]; + // dp[0] = costs[n-1][0]; + // dp[1] = costs[n-1][1]; + // dp[2] = costs[n-1][2]; + + // for (int i= n-2; i>=0; i--){ + // int tempR = dp[0]; + // dp[0] = costs[i][0] + Math.min(dp[1],dp[2]); + // int tempB = dp[1]; + // dp[1] = costs[i][1] + Math.min(tempR,dp[2]); + // dp[2] = costs[i][2] + Math.min(tempR, tempB); + + + // } + // return Math.min(dp[0], Math.min(dp[1],dp[2])); + // } + + //time: O(n) + // public int minCost(int[][] costs) { + // int n = costs.length; + // int[][] dp = new int[n][3]; + // dp[n-1][0] = costs[n-1][0]; + // dp[n-1][1] = costs[n-1][1]; + // dp[n-1][2] = costs[n-1][2]; + + // for (int i= n-2; i>=0; i--){ + // dp[i][0] = costs[i][0] + Math.min(dp[i+1][1],dp[i+1][2]); + // dp[i][1] = costs[i][1] + Math.min(dp[i+1][0],dp[i+1][2]); + // dp[i][2] = costs[i][2] + Math.min(dp[i+1][1],dp[i+1][0]); + + + // } + // return Math.min(dp[0][0], Math.min(dp[0][1],dp[0][2])); + // } + + //time: O(2^n) + // public int minCost(int[][] costs) { + // int colorR = helper(costs, 0, 0); + // int colorB = helper(costs, 0, 1); + // int colorG = helper(costs, 0, 2); + // return Math.min(colorR, Math.min(colorB, colorG)); + // } + // private int helper(int[][] costs, int idx, int col) { + // //baes + // if (idx == costs.length) return 0; + // //logic + // if(col==0){ + // return costs[idx][0]+ Math.min(helper(costs,idx+1,1), helper(costs,idx+1,2)); + // } + // if(col==1){ + // return costs[idx][1]+ Math.min(helper(costs,idx+1,0), helper(costs,idx+1,2)); + // } + // if(col==2){ + // return costs[idx][2]+ Math.min(helper(costs,idx+1,0), helper(costs,idx+1,1)); + // } + // return -1; + // } +} From e6db17eee938696afc78bd8b85d2bc94c87a7ce0 Mon Sep 17 00:00:00 2001 From: Joshua Yoon Date: Mon, 15 Jun 2026 15:53:28 -0500 Subject: [PATCH 3/3] push dp-2 --- CoinChange2.java | 1 + PaintHouse.java | 1 + PaintHouse2.java | 1 + 3 files changed, 3 insertions(+) diff --git a/CoinChange2.java b/CoinChange2.java index 9ba66a1a..c512ded9 100644 --- a/CoinChange2.java +++ b/CoinChange2.java @@ -1,4 +1,5 @@ public class CoinChange2 { + public int change(int amount, int[] coins) { int n = coins.length; int m = amount; diff --git a/PaintHouse.java b/PaintHouse.java index 80fe82c6..658998c3 100644 --- a/PaintHouse.java +++ b/PaintHouse.java @@ -1,5 +1,6 @@ public class PaintHouse { //time: O(n) + public int minCost(int[][] costs) { int n = costs.length; // int[] dp = new int[3]; diff --git a/PaintHouse2.java b/PaintHouse2.java index 0fa15d3b..54c7c6b4 100644 --- a/PaintHouse2.java +++ b/PaintHouse2.java @@ -1,6 +1,7 @@ public class PaintHouse2 { public int paintHouse2(int[][] costs) { + // R G B // 0 1 2 for (int i = 1; i < costs.length; i++) {