From 8323a3f79ebc8043824dc4824d3850946acbda80 Mon Sep 17 00:00:00 2001 From: tmujje Date: Tue, 4 May 2021 20:52:10 -0400 Subject: [PATCH 1/2] DP-1 Assignment submission --- CoinChange.java | 39 +++++++++++++++++++++++++++++++++++++++ HouseRobber.java | 20 ++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 CoinChange.java create mode 100644 HouseRobber.java diff --git a/CoinChange.java b/CoinChange.java new file mode 100644 index 00000000..f0fc809e --- /dev/null +++ b/CoinChange.java @@ -0,0 +1,39 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, but in C# I felt the 2D arrays are a bit tricky and not intutional. So +// I decided to choose java as my language for coding these parts on leet code. + +//Time Complexity : O(m * n) since we are iterating through the matrix +//Space Complexity: O(m * n) since we are taking extra 2D array + +public int coinChange(int[] coins, int amount) { + if(coins == null || coins.length == 0) return 0; + + var dp = new int[coins.length + 1][amount + 1]; + + //fill first column + for(int i=0; i < dp.length; i++){ + dp[i][0] = 0; + } + + //fill first row + for(int j = 1; j < dp[0].length; j++){ + dp[0][j] = amount + 1; + } + + //iterate and fill the dp matrix + for(int i = 1; i < dp.length; i++){ + for(int j=1; j < dp[0].length; j++){ + + // Until the amount equals denomination, min will be just the case above + if(j < coins[i-1]){ + //zero case + dp[i][j] = dp[i-1][j]; + } + else{ + dp[i][j] = Math.min(dp[i-1][j], dp[i][j - coins[i-1]] + 1); + } + } + } + var result = dp[dp.length-1][dp[0].length-1]; + return result >= amount+1 ? -1 : result; + } diff --git a/HouseRobber.java b/HouseRobber.java new file mode 100644 index 00000000..94940c68 --- /dev/null +++ b/HouseRobber.java @@ -0,0 +1,20 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are iterating through the original array +//Space Complexity: O(1) since we are just using variables and not any extra dp array + +public int rob(int[] nums) { + + if(nums==null || nums.length == 0) return 0; + + int skip = 0; + int take = nums[0]; + for(int i = 1; i < nums.length; i++){ + int temp = skip; + // The amount for not choosing part of the next house will be equal to the Max amount of previous skip and take + skip = Math.max(skip,take); + take= nums[i]+ temp; + } + return Math.max(skip,take); + } \ No newline at end of file From 483c3283b5d2f5038fa00bf312eb896f033b4ba2 Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Fri, 7 Aug 2026 23:32:18 -0400 Subject: [PATCH 2/2] Complete DP-1 assignment --- CoinChange.java | 64 +++++++++++++++++++++++++----------------------- HouseRobber.java | 42 ++++++++++++++++++------------- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/CoinChange.java b/CoinChange.java index f0fc809e..bd6e0e4c 100644 --- a/CoinChange.java +++ b/CoinChange.java @@ -1,39 +1,41 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No, but in C# I felt the 2D arrays are a bit tricky and not intutional. So -// I decided to choose java as my language for coding these parts on leet code. +// Approach - Recursion can be used to pursue exhaustive path, but since this problem has repeated sub problems +// we could use Dynamic programming. +//Time Complexity - O(mxn) +//Space Complexity - O(mxn) -//Time Complexity : O(m * n) since we are iterating through the matrix -//Space Complexity: O(m * n) since we are taking extra 2D array +class CoinChange { + public int coinChange(int[] coins, int amount) { -public int coinChange(int[] coins, int amount) { - if(coins == null || coins.length == 0) return 0; - - var dp = new int[coins.length + 1][amount + 1]; - - //fill first column - for(int i=0; i < dp.length; i++){ - dp[i][0] = 0; - } - - //fill first row - for(int j = 1; j < dp[0].length; j++){ - dp[0][j] = amount + 1; - } - - //iterate and fill the dp matrix - for(int i = 1; i < dp.length; i++){ - for(int j=1; j < dp[0].length; j++){ - - // Until the amount equals denomination, min will be just the case above - if(j < coins[i-1]){ - //zero case + //Validate the inputs + if(coins == null || coins.length == 0){ + return 0; + } + + int m = coins.length; + int n = amount; + + int[][] dp = new int[m+1][n+1]; + + // Fill first row coins[0][j] with any amount higher than the given amount + for (int j = 1; j <= n; j++){ + dp[0][j] = amount + 1; + } + + for(int i = 1; i <= m; i++) + { + for(int j = 1; j <= n; j++) + { + // If we don't have the availability of choose case when amount is less than the denomination, copy the above case + 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); + else + { + dp[i][j] = Math.min(dp[i-1][j], dp[i][j-coins[i-1]]+1); } } } - var result = dp[dp.length-1][dp[0].length-1]; - return result >= amount+1 ? -1 : result; + if(dp[m][n] == amount+1) return -1; + return dp[m][n]; } +} \ No newline at end of file diff --git a/HouseRobber.java b/HouseRobber.java index 94940c68..bd972768 100644 --- a/HouseRobber.java +++ b/HouseRobber.java @@ -1,20 +1,28 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No +// Approach - Recursion can be used to pursue exhaustive path, but since this problem has repeated sub problems +// we could use Dynamic programming. +//Time Complexity: O(n) +//Space Complexity: O(n) -//Time Complexity : O(n) since we are iterating through the original array -//Space Complexity: O(1) since we are just using variables and not any extra dp array +class HouseRobber { + public int rob(int[] nums) { -public int rob(int[] nums) { - - if(nums==null || nums.length == 0) return 0; - - int skip = 0; - int take = nums[0]; - for(int i = 1; i < nums.length; i++){ - int temp = skip; - // The amount for not choosing part of the next house will be equal to the Max amount of previous skip and take - skip = Math.max(skip,take); - take= nums[i]+ temp; + if (nums.length == 0){ + return 0; } - return Math.max(skip,take); - } \ No newline at end of file + + if (nums.length == 1) return nums[0]; + + int n = nums.length; + int [] dp = new int[n]; + + dp[0] = nums[0]; + dp[1] = Math.max(nums[0],nums[1]); + + //Maximum between choose case vs non choose case where we cannot select the immediate neighbouring house. + for(int i=2 ; i