Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -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]));
}
}
20 changes: 20 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -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];
}
}