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
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DP-1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
69 changes: 69 additions & 0 deletions CoinChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Time Complexity : O(m*n)
// Space Complexity : O(m*n)
// Did this code successfully run on Leetcode :yes.

// Any problem you faced while coding this :

// Your code here along with comments explaining your approach

public class CoinChange {

// Time c omplexity O(m x n)
// space complexity O(m x n)
public int coinChange(int[] coins, int amount) {
// edge case
if (coins == null || coins.length == 0)
return 0;
int[][] dp = new int[coins.length + 1][amount + 1]; // because of 0
for (int i = 1; i < dp[0].length; i++)
dp[0][i] = 9999;
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (j < coins[i - 1]) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - coins[i - 1]] + 1);
}
}
}
int result = dp[dp.length - 1][dp[0].length - 1];
if (result >= 9999)
return -1;
return result;
}

/*
*
// Time c omplexity O(m x n)
// space complexity O(m x n)
*
public int coinChange(int[] coins, int amount) {
int m= coins.length, n=amount;
int [][] dp= new int[m+1][n+1];

dp[0][0]=0;

// top row
for (int j=1; j<=n;j++){
dp[0][j]=Integer.MAX_VALUE-2;
}

for (int i=1; i<=m;i++){
for (int j=1; j<=n;j++){
if (j<coins[i-1]){
//not choose case
dp[i][j]=dp[i-1][j];
}else{
dp[i][j]=Math.min(dp[i-1][j],1+dp[i][j-coins[i-1]]);
}
}
}
int resp=dp[m][n];
if (resp>=Integer.MAX_VALUE-2)
return -1;
return resp;
}

*/

}
54 changes: 54 additions & 0 deletions HouseRobber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Time Complexity : O(n)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode :yes.

// Any problem you faced while coding this :

// Your code here along with comments explaining your approach

public class HouseRobber {

public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
if (nums.length == 1)
return nums[0];
if (nums.length == 2)
return Math.max(nums[0], nums[1]);
int cache[] = new int[nums.length];
cache[0] = nums[0];
cache[1] = nums[1];
cache[2] = cache[0] + nums[2];

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

}


// time complexity O(n)
// Space complexity O(1)
// just using two variable and temp variable to store prev robbed, this is more efficient
/*public int rob(int[] nums) {
if (nums==null || nums.length==0)
return 0;
int robbed=0;
int notRobbed=0;
for (int i=0;i<nums.length;i++){
int pRobbed=robbed;
robbed=nums[i]+notRobbed;
notRobbed=Math.max(pRobbed,notRobbed);

}
return Math.max(robbed,notRobbed);

}*/


public static void main(String[] args) {

}

}