Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem 1 #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions Problem
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
I currently have difficulty in coding out dp problems. Will update the code after the class
Howvever for the coin change problem , I have an approach in mind that I think should work.

There are 2 cases for each coin. Either that coin will be included in the sum that we have to achieve or that coin will not be included.

coin(amount , n) -> let this denote the amount that can be achieved using n coins.
= coin(amount- j, n) + 1 if ith coin with value j is included
or coin(amount, n-1) if ith coin is not included


40 changes: 40 additions & 0 deletions Robber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this : took help from geeksforgeeks

// First 3 cases handle the scenarios when we have 0 or 1 or 2 houses
// Then for more than 3 houses, at every house we have to take a decision to rob that house or not. We take this decision
// by calculating maximum of ( money robbed till 2 houses before that plus money in that house or money robbed till one house
// before it)


class Solution {
public int rob(int[] nums) {

if(nums.length==0 || nums==null)
return 0;

if(nums.length==1)
return nums[0];

if(nums.length==2)
return Math.max(nums[0],nums[1]);


int[] dp = new int[nums.length];

//Base Case
dp[0] = nums[0];
dp[1] = Math.max(nums[0],nums[1]);

for(int i =2; i< nums.length ; i++)
{
dp[i] = Math.max(nums[i] + dp[i-2], dp[i-1]);

}

return dp[nums.length-1];

}
}