diff --git a/Coin change 2 b/Coin change 2 new file mode 100644 index 00000000..b10fcbb7 --- /dev/null +++ b/Coin change 2 @@ -0,0 +1,30 @@ +Time complexity - O(nm) where n is number of coins and m is the amount +Space complexity - O(nm) + +class Solution { +public: + int change(int amount, vector& coins) { + int numberOfCoins=coins.size(); + + int matrix[numberOfCoins+1][amount+1]; + + for (int i=0;i<=amount;i++) { + matrix[0][i]=0; + } + for (int i=0;i<=coins.size();i++) { + matrix[i][0]=1; + } + + for (int i=1;i<=numberOfCoins;i++) { + for (int j=1;jj) { + matrix[i][j]=matrix[i-1][j]; + } else if (coins[i-1]<=j){ + matrix[i][j]=matrix[i-1][j] + matrix[i][j-coins[i-1]]; + } + } + } + return matrix[numberOfCoins][amount]; + + } +}; diff --git a/paint house b/paint house new file mode 100644 index 00000000..a172cebc --- /dev/null +++ b/paint house @@ -0,0 +1,33 @@ +// Time Complexity - O(3n) where n is the number of houses +// Space Complexity - O(3) +class Solution { +public: + int minCost(vector>& costs) { + int rows = costs.size(); + int cols = costs[0].size(); + + vector> dp(rows, vector(cols, 0)); + + for (int i=0;i