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
107 changes: 107 additions & 0 deletions Leetcode_256.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//way1
//Let's try to explore all the flows. And identify the minimum possible route
//Tc: m*(2^n)
class Solution {
public int minCost(int[][] costs) {
int case1=helper(costs,0,0);

int case2=helper(costs,0,1);

int case3=helper(costs,0,2);

return Math.min(case1,Math.min(case2,case3));

}

private int helper(int[][] costs, int i, int j){
//base case
if(i==costs.length) return 0;


//logic
if(j==0){
return costs[i][j] + Math.min(helper(costs, i+1, 1),
helper(costs,i+1,2));
}else if(j==1){
return Math.min(costs[i][j]+helper(costs,i+1,0),
costs[i][j]+helper(costs,i+1,2));
}else{
return Math.min(costs[i][j]+helper(costs,i+1,0),
costs[i][j]+helper(costs,i+1,1));
}
}
}

//way2
//Let's try to explore all the flows. And identify the minimum possible route -- causing time limit exceeded
//So, doing memoization. Trying to remember prev results
//Tc: m*n; SC: m*n
class Solution {
public int minCost(int[][] costs) {

int[][] memo=new int[costs.length][3];
int case1=helper(costs,0,0,memo);

int case2=helper(costs,0,1,memo);

int case3=helper(costs,0,2,memo);

return Math.min(case1,Math.min(case2,case3));

}

private int helper(int[][] costs, int i, int j,int[][] memo){
//base case
if(i==costs.length) return 0;

if(memo[i][j]!=0) return memo[i][j];


//logic
if(j==0){
memo[i][j]= costs[i][j] + Math.min(helper(costs, i+1, 1,memo),
helper(costs,i+1,2,memo));
return memo[i][j];
}else if(j==1){
memo[i][j] = Math.min(costs[i][j]+helper(costs,i+1,0,memo),
costs[i][j]+helper(costs,i+1,2,memo));
return memo[i][j];
}else{
memo[i][j] = Math.min(costs[i][j]+helper(costs,i+1,0,memo),
costs[i][j]+helper(costs,i+1,1,memo));
return memo[i][j];
}
}
}

//way3:
//Tabulation
//Tracking all the flows and identifying the min at every step
//Returning min at last row
//TC: O(m*n); SC: O(m*n)

class Solution {
public int minCost(int[][] costs) {
int n=costs.length;

int[][] dp=new int[n][3];
dp[0][0]=costs[0][0];
dp[0][1]=costs[0][1];
dp[0][2]=costs[0][2];

for(int i=1;i<n;i++){
for(int j=0;j<3;j++){
if(j==0){
dp[i][j]=costs[i][j]+Math.min(dp[i-1][1],dp[i-1][2]);
}else if(j==1){
dp[i][j]=costs[i][j]+Math.min(dp[i-1][0],dp[i-1][2]);
}else{
dp[i][j]=costs[i][j]+Math.min(dp[i-1][0],dp[i-1][1]);
}
}
}

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

}
}
119 changes: 119 additions & 0 deletions Leetcode_518.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//way1
//Exhaustive-- choose all the combinations. If the combinations is equals to target, add it to result
//TC: 2^(m+n)
class Solution {
public int change(int amount, int[] coins) {
return helper(amount,coins,0);

}
private int helper(int amount, int[] coins, int i){
//base case
if(amount==0) return 1;

if(amount<0 || i==coins.length){
return 0;
}

//logic
//no choose
int case1=helper(amount,coins,i+1);

int case2=helper(amount-coins[i], coins, i);

return case1+case2;
}
}

//way2:
//Exhaustive-- choose all the combinations. If the combinations is equals to target, add it to result -- causing time limit exceeded
//Memoization-- remembering prev results and using them.
//TC: O(m*n); SC: O(m*n)
class Solution {
public int change(int amount, int[] coins) {
int[][] memo=new int[coins.length+1][amount+1];
for(int i=0; i<=coins.length; i++){
Arrays.fill(memo[i], -1);
}
return helper(amount,coins,0,memo);

}
private int helper(int amount, int[] coins, int i,int[][] memo){
//base case
if(amount==0) return 1;

if(amount<0 || i==coins.length){
return 0;
}

if(memo[i][amount]!=-1) return memo[i][amount];

//logic
//no choose
int case1=helper(amount,coins,i+1,memo);

int case2=helper(amount-coins[i], coins, i,memo);

memo[i][amount]=case1+case2;

return case1+case2;
}
}

//way3:
//Tabulation
//Identify number of combinations for amount 0 to target with the given coins
//return the last element of dp array
//tc: o(m*n); sc:o(m*n)

class Solution {
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++){
if(j<coins[i-1]){
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];
}
}

//way4:
//Tabulation
//Identify number of combinations for amount 0 to target with the given coins
//return the last element of dp array
//tc: o(m*n); sc:o(n)

class Solution {
public int change(int amount, int[] coins) {
int n=coins.length;
int m=amount;

int[] dp=new int[m+1];

dp[0]=1;

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

return dp[m];
}
}