Skip to content
Open

DP 2 #1774

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
94 changes: 94 additions & 0 deletions CoinChange2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Time Complexity : O(2pow(amount+(amount/smallest coin))
// Space Complexity : O(2pow(amount+(amount/smallest coin))
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : TLE


// Your code here along with comments explaining your approach
// We follow the before approach either to take or not take the current coin in recursion
class Solution {
public int change(int amount, int[] coins) {
int[][] mem = new int[coins.length][amount+1];
return helper(amount,coins,0,mem);
}

private int helper(int amount, int[] coins, int index, int[][] mem)
{
//base case
if(amount==0)
{
return 1;
}
if(amount<0 || index>=coins.length)
{
return 0;
}
if(mem[index][amount]){}
//if current coin is taken
int case1 = helper(amount-coins[index],coins,index);
//if current coin is not taken
int case2 = helper(amount,coins,index+1);
return case1+case2;
}
}

// Time Complexity : O(coins len * amount)
// Space Complexity : O(coins len * amount)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : TLE


// Your code here along with comments explaining your approach
// We follow the before approach either to take or not take the current coin in recursion with memorization
// still TLE
class Solution {
public int change(int amount, int[] coins) {
int[][] mem = new int[coins.length][amount+1];
return helper(amount,coins,0,mem);
}

private int helper(int amount, int[] coins, int index,int[][] mem)
{
//base case
if(amount==0)
{
return 1;
}
if(amount<0 || index>=coins.length)
{
return 0;
}
//use memorized solution if exists
if(mem[index][amount]!=0)
{
return mem[index][amount];
}
//if current coin is taken
int case1 = helper(amount-coins[index],coins,index,mem);
//if current coin is not taken
int case2 = helper(amount,coins,index+1,mem);
int pathPossible = case1+case2;
//put to memorized solution
mem[index][amount] = pathPossible;
return pathPossible;
}
}

// Time Complexity : O( amount)
// Space Complexity : O(amount)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Yes
class Solution {
public int change(int amount, int[] coins) {
int[] mem = new int[amount+1];
mem[0] = 1;
for(int coin : coins)
{
for(int i = coin;i<=amount;i++)
{
mem[i]+=mem[i-coin];
}
}
return mem[amount];
}
}
25 changes: 25 additions & 0 deletions PaintHouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity : O(costs len)
// Space Complexity : O(costs len)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Yes


// Your code here along with comments explaining your approach
// We start bottom up, calculate at each step what's the min possible by taking prev sides sum
class Solution {

public int minCost(int[][] costs) {
//edge case
if (costs.length == 0) return 0;

int[][] memo = new int[costs.length + 1][3];
//bottom-up, taking the min sum from prev two
for (int i = costs.length - 1; i >= 0; --i) {
memo[i][0] = costs[i][0] + Math.min(memo[i + 1][1], memo[i + 1][2]);
memo[i][1] = costs[i][1] + Math.min(memo[i + 1][0], memo[i + 1][2]);
memo[i][2] = costs[i][2] + Math.min(memo[i + 1][0], memo[i + 1][1]);
}
//return min of top
return Math.min(Math.min(memo[0][0], memo[0][1]), memo[0][2]);
}
}