From 286dbb6a9538e0d9544fb1cd57459d6055c18682 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Wed, 22 Apr 2026 12:16:34 -0700 Subject: [PATCH] DP1 --- HouseRobber.java | 41 ++++++++++++++++++++++++++++++++++++ Sample.java | 55 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 HouseRobber.java diff --git a/HouseRobber.java b/HouseRobber.java new file mode 100644 index 00000000..b068cb9f --- /dev/null +++ b/HouseRobber.java @@ -0,0 +1,41 @@ +// Time Complexity : O(n) where n is the number of houses +// Space Complexity : O(1) as we are using only two variables +// 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 +// As discussed in class, Greedy or selective approach will not work here, +// as we might end up with a local optimal solution. +// We have to go with exhaustive approach, +// where we will try all the combinations of houses to make up the amount. +// But it will lead to exponential time complexity, +// so we will use dynamic programming to optimize it. +// The input variable is nums, which is changing, so we will create a dp array of size n +// where n is the number of houses. +// For every house, if we choose to rob it, +// we will take the value of that house and add it to the value of the house two steps +// back, as we cannot rob adjacent houses. +// If we choose not to rob it, we will take the value from the previous house. +// Lastly, we will return the value at the last index of the dp array, which will give us the maximum amount we can rob. + + +public class HouseRobber { + class Solution { + public int rob(int[] nums) { + if (nums.length < 2) return nums[0]; + + int n = nums.length; + int[] dp = new int[n]; + + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + for (int i = 2; i < n; i++) { + dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]); + } + + return dp[n - 1]; + } +} +} diff --git a/Sample.java b/Sample.java index 1739a9cb..6e34d119 100644 --- a/Sample.java +++ b/Sample.java @@ -1,7 +1,54 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : +// Time Complexity : O(m*n) where m is the number of coins and n is the amount +// Space Complexity : o(m*n) where m is the number of coins and n is the amount +// Did this code successfully run on Leetcode :yes +// Any problem you faced while coding this : not exactly, but I had to be careful +// with the initialization of the dp array to avoid overflow issues. // Your code here along with comments explaining your approach +// As discussed in class, Greedy or selective approach will not work here, +// as we might end up with a local optimal solution. +// We have to go with exhaustive approach, +// where we will try all the combinations of coins to make up the amount. +// But it will lead to exponential time complexity, +// so we will use dynamic programming to optimize it. +// The input variables are coins and amount,which are changing, so we will create a dp array of size (m+1) x (n+1) where m is the number of coins and n is the amount. +// For every amount, if 0 - not choose we will take the value from the previous row, +// if we choose the coin, we will take 1 + value from the same row and column - coin value. +// Lastly, we will return the value at the bottom right corner of the dp array, if it is still the initialized value, we will return -1, +// otherwise we will return that value. +class Solution { + public int coinChange(int[] coins, int amount) { + + int m = coins.length; + int n = amount; + + int[][] dp = new int[m + 1][n + 1]; + + dp[0][0] = 0; + + // Initialize top row with a large value + for (int j = 1; j <= n; j++) { + dp[0][j] = Integer.MAX_VALUE - 10; // avoid overflow + } + + for (int i = 1; i <= m; i++) { + for (int j = 0; j <= n; j++) { + + if (j < coins[i - 1]) { + 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 res = dp[m][n]; + + if (res == Integer.MAX_VALUE - 10) return -1; + return res; + } +} \ No newline at end of file