diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..3f3893af --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 00000000..a54c51d3 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + DP-1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/CoinChange.java b/CoinChange.java new file mode 100644 index 00000000..0bed7f9d --- /dev/null +++ b/CoinChange.java @@ -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=Integer.MAX_VALUE-2) + return -1; + return resp; + } + + */ + +} \ No newline at end of file diff --git a/HouseRobber.java b/HouseRobber.java new file mode 100644 index 00000000..c6886d61 --- /dev/null +++ b/HouseRobber.java @@ -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