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
34 changes: 34 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity : O(MXN) M is the total number of coins and N is the lenght of Array
// Space Complexity : O(N)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this :Initialise the Array with Max Value when looking for the Minimum


// Your code here along with comments explaining your approach
class Solution{
int coinChange(int coins[], int amount){
//Edge Case
if(amount<1){
return 0;
}
else{
// Make an array starting from 0 to the Max amount in the array so that at each position we check the mimum coints to reach that particular value
int minCoinsDP[]=new int[amount+1];
for(int i=1; i<minCoinsDP.length;i++){
minCoinsDP[i]=Integer.MAX_VALUE;
for(int coin: coins){
// i Will run from start to the Coing value as with a coin value bigger then i we cannot make the amount.
// If We are able to make a value with smaller denomation we add that and the previous answer of smaller denomation in the minimum
if(coin<=i && minCoinsDP[i-coin]!=Integer.MAX_VALUE){
minCoinsDP[i]=Math.min(minCoinsDP[i], minCoinsDP[i-coin]+1);
}
}
}
// No Amount was able to make with the coin denomaitions.
if(minCoinsDP[amount]==Integer.MAX_VALUE){
return -1;
}
return minCoinsDP[amount];
}
}
}
26 changes: 26 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity : O(N) Each House is Visited
// Space Complexity :O(Dp Array) We make extra array
// 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
class Solution {
public int rob(int[] nums) {
// If no houses
if (nums.length == 0) return 0;
// If only one house
if (nums.length == 1) return nums[0];
// dp[i] = max money we can rob till house i
int[] dp = new int[nums.length];
// Base case
dp[0]=nums[0];
dp[1]=Math.max(nums[0], nums[1]);
// Fill dp arr
for (int i=2;i<nums.length;i++) {
dp[i]= Math.max(dp[i-1],nums[i]+dp[i-2]);
}
// Answer is last value
return dp[nums.length - 1];
}
}