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
37 changes: 37 additions & 0 deletions CoinChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity : O(mxn)
// Space Complexity : O(amount)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :no


// Your code here along with comments explaining your approach

//Using the DP we will set the for each coin we will asign hingher value than amount and
//find the minimum coins for amount j is either the old answer, or using this coin plus
// the best answer for the remaining amount.

class CoinChange{
public int coinChange(int[] coins, int amount) {

int n = coins.length;
int []dp = new int[amount+1];

for(int i = 1; i <= amount; i++){
dp[i] = amount+1;
}

for(int i = 1; i <= n; i++){
for(int j = 1; j <= amount; j++){
if(coins[i-1] <= j){
dp[j] = Math.min(dp[j], dp[j-coins[i-1]]);
}
}
}

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

return dp[amount];
}
}
19 changes: 19 additions & 0 deletions HouseRobber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class HouseRobber {
public int rob(int[] nums) {

int n = nums.length;

if(n == 1) return nums[0];
int prev = nums[0];
int curr = Math.min(prev, nums[1]);

for(int i = 2; i < n; i++){
int temp =curr;
curr = Math.max(prev+nums[i], curr);
prev = curr;
}

return curr;

}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.