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
26 changes: 26 additions & 0 deletions CoinChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class CoinChange<T> {

public int coinChange(int[] coins, int amount) {
int col = amount;
int[] dp = new int[col+1];
for (int i=1; i<col+1; i++){
dp[i] = amount+1;
}
for (int i=1; i<coins.length+1; i++){
for (int j=1; j<col+1; j++){
// if denomination > amount -> num from above
if (coins[i-1] > j){
dp[j] = dp[j];
}
// get min of above and i-denomnination +1
else{
dp[j] = Math.min(dp[j],1+ dp[j-coins[i-1]]);
}
}
}
int ans = dp[col];
if (ans > amount) return -1;
return ans;
}

}
21 changes: 21 additions & 0 deletions HoseRobber2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class HoseRobber2 {

public int robber(int[] nums) {
int[][] dp = new int[nums.length + 1][2];
dp[0][1] = 0;
dp[1][0] = 0;
int max = Integer.MIN_VALUE;

for (int row = 1; row <= nums.length; row++) {
int notTaken = Math.max(dp[row - 1][0], dp[row - 1][1]);
int taken = dp[row - 1][0] + nums[row - 1];
max = Math.max(max, Math.max(notTaken, taken));

dp[row][0] = notTaken;
dp[row][1] = taken;
}

return max;

}
}
55 changes: 55 additions & 0 deletions HouseRobber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class HouseRobber {
//T = O(n)

//space = O(1)
public int rob(int[] nums) {
if (nums.length ==1) return nums[0];
int n = nums.length;
// int[] dp = new int[n];
int prev = nums[0];
int curr = Math.max(nums[0],nums[1]);
for (int i=2; i<n; i++){
int temp = curr;
curr= Math.max(curr,prev+nums[i]);
prev = temp;
}
return curr;

}
}

// class Solution {
// //T = O(n)
// //space = O(n)
// public int rob(int[] nums) {
// if (nums.length ==1) return nums[0];
// int n = nums.length;
// int[] dp = new int[n];
// dp[0] = nums[0];
// dp[1] = Math.max(nums[0],nums[1]);
// for (int i=2; i<n; i++){
// dp[i] = Math.max(dp[i-1],dp[i-2]+nums[i]);
// }
// return dp[n-1];

// }
// }


//O(2^n)
// class Solution {
// public int rob(int[] nums) {
// if (nums.length ==1) return nums[0];
// return helper(nums, 0);
// }
// private int helper(int[] nums, int idx) {
// //base
// if (idx >= nums.length) return 0;

// //logic
// int case0 = helper(nums, idx+1);
// int case1 = nums[idx] + helper(nums,idx+2);

// return Math.max(case0, case1);
// }
// }