diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..7193f866 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,26 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +class Solution { + public int minCost(int[][] costs) { + // If nohouses + if (costs==null|| costs.length==0) return 0; + int n = costs.length; + int[][]dp=new int[n][3]; + // Base casefirst house cost stays same + dp[0][0] = costs[0][0]; //red + dp[0][1] = costs[0][1]; //blue + dp[0][2] = costs[0][2]; //green + // Fill dp table + for (int i = 1; i < n; i++) { + //Dp[i] the number of ways to make amout 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][0],dp[i-1][1]); + } + // Minimum of last house + return Math.min(dp[n - 1][0], + Math.min(dp[n - 1][1], dp[n - 1][2])); + } +} diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..46052388 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,20 @@ + +// Time Complexity : O(amount * number of coins) +// Space Complexity : O(amount) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +class Solution { + public int change(int amount, int[] coins) { + // dp[i] = number of ways to make amount i + int[]dp=new int[amount+1]; + // Base case + dp[0]=1; + // For each coin + for (int coin : coins) { + for (int i=coin; i<= amount; i++) { + dp[i]=dp[i]+dp[i-coin]; + } + } + return dp[amount]; + } +} \ No newline at end of file