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
76 changes: 76 additions & 0 deletions Leetcode256.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class Solution {
public int minCost(int[][] costs) {
// // Solution 1 = 2D approach
// // TC = O(3n) = O(n)
// // SC = O(3n) = O(n)
// int n = costs.length;
// int[][] dp = new int[n][3];
// int[][] bestPath = new int[n][3]; // not a requirement of this problem. Just
// wanted to solve best path as well.

// for(int j=0; j<3; j++) {
// dp[0][j] = costs[0][j];
// }
// for(int i=1; i<n;i++) {
// int sum = 0; // not a requirement of this problem. Just wanted to solve best
// path as well.
// //Red

// if(dp[i-1][1] > dp[i-1][2]) {
// sum = dp[i-1][2];
// bestPath[i][0] = 2;
// } else {
// sum = dp[i-1][1];
// bestPath[i][0] = 1;
// }
// dp[i][0] = costs[i][0]+ sum;

// //Blue
// if(dp[i-1][0] > dp[i-1][2]) {
// sum = dp[i-1][2];
// bestPath[i][1] = 2;
// } else {
// sum = dp[i-1][0];
// bestPath[i][1] = 0;
// }
// dp[i][1] = costs[i][1]+ sum;

// //Green
// if(dp[i-1][1] > dp[i-1][0]) {
// sum = dp[i-1][0];
// bestPath[i][2] = 0;
// } else {
// sum = dp[i-1][1];
// bestPath[i][2] = 1;
// }
// dp[i][2] = costs[i][2]+ sum;

// }
// // System.out.println("BesPath: " + Arrays.deepToString(bestPath));

// return Math.min(dp[n-1][0], Math.min(dp[n-1][1], dp[n-1][2]));

// Solution 2 - 1D DP array
int n = costs.length;
int redIdx = costs[0][0];
int blueIdx = costs[0][1];
int greenIdx = costs[0][2];

for (int i = 1; i < n; i++) {
int tempRed = redIdx;
// Red
redIdx = costs[i][0] + Math.min(blueIdx, greenIdx);

int tempBlue = blueIdx;
// Blue
blueIdx = costs[i][1] + Math.min(tempRed, greenIdx);

// Green
greenIdx = costs[i][2] + Math.min(tempRed, tempBlue);

}

return Math.min(redIdx, Math.min(blueIdx, greenIdx));

}
}
45 changes: 45 additions & 0 deletions Leetcode518.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public int change(int amount, int[] coins) {
// //Solution 1 = 2D DP Matrix
// // TC : O(nm)
// // SC : O(nm)
// int m = coins.length;
// int n = amount;
// int[][] dp = new int[m+1][amount+1]; // 0 row and 0 amount
// dp[0][0] = 1;

// for(int i=1; i<= m; i++) {
// for(int j =0; j<=n; j++) {
// if(j<coins[i-1]) {

// dp[i][j] = dp[i-1][j];
// } else {
// dp[i][j] = dp[i][j-coins[i-1]] + dp[i-1][j];
// }

// }
// }

// return dp[m][n];

// Solution 1 = 2D DP Matrix
// TC : O(n)
// SC : O(n)
int m = coins.length;
int n = amount;
int[] dp = new int[amount + 1]; // 0 row and 0 amount
dp[0] = 1;

for (int i = 1; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (j >= coins[i - 1]) {
dp[j] = dp[j - coins[i - 1]] + dp[j];
}

}
}

return dp[n];

}
}