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
32 changes: 32 additions & 0 deletions CoinChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
// TC - O(n * m)
// SC - O(n * m)
public int coinChange(int[] coins, int amount) {
int n = coins.length;
int m = amount;

//initialize dp array to store the min required coins, the first row is initialized to maximum value for comparison purposes
int[][] dp = new int[n + 1][m + 1];
for (int j = 0; j <= m; j++) {
dp[0][j] = amount + 1;
}

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
// if the amount at current index is less than the coins[i - 1] value (if amount is 2 and coind value is 5) then we just take value from the row
if (j < coins[i - 1]) {
dp[i][j] = dp[i-1][j];
} else {
// else we calculate minimum of the value row above has and the total of value at index position
dp[i][j] = Math.min(dp[i-1][j], dp[i][j - coins[i - 1]] + 1);
}
}
}

if (dp[n][m] > amount) {
return -1;
}

return dp[n][m];
}
}
17 changes: 17 additions & 0 deletions HouseRobber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
//SC - O(1)
//TC - O(n)
public int rob(int[] nums) {
int n = nums.length;
int prev = 0;
int curr = nums[0];

for (int i = 1; i < n; i++) {
int temp = curr; // store curr value in prev
curr = Math.max(curr, prev + nums[i]); // calc maximum of robbing the current house (prev + nums[i]) and the value of previous house (curr)
prev = temp;
}

return curr;
}
}