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
25 changes: 25 additions & 0 deletions CoinChange2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity : O(n * amount)
// Space Complexity : O(amount)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Needed to avoid counting duplicate permutations, so coins are processed one by one.

class CoinChange2 {
public int change(int amount, int[] coins) {
int[] dp = new int[amount + 1];

// Base case: There is 1 way to make amount 0
dp[0] = 1;

// Process each coin
for (int i = 1; i <= coins.length; i++) {
int coin = coins[i - 1];

// Update ways for all amounts from coin to target amount
for (int j = coin; j <= amount; j++) {
dp[j] += dp[j - coin];
}
}

return dp[amount];
}
}
35 changes: 35 additions & 0 deletions PaintHouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Needed to store old dp values before updating them.


class PaintHouse{
public int minCost(int[][] costs) {

int n = costs.length;

// dp[0], dp[1], dp[2] store min cost if current house is painted
// red, blue, or green respectively
int dp [] = new int[3];

// Start from the last house
dp[0] = costs[n-1][0];
dp[1] = costs[n-1][1];
dp[2] = costs[n-1][2];

// Move from second last house to first house
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[2], dp[1]));


}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.