From b83ae140062a924aff7690d7efd05e2ba2185a5c Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sun, 4 Jan 2026 13:12:37 -0600 Subject: [PATCH 1/2] Add multiple solutions for house robber problem Implemented four different approaches to solve the house robber problem, including recursion, memoization, and dynamic programming. --- Leetcode_198.java | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Leetcode_198.java diff --git a/Leetcode_198.java b/Leetcode_198.java new file mode 100644 index 00000000..e0d278bc --- /dev/null +++ b/Leetcode_198.java @@ -0,0 +1,110 @@ +//Way:1 +//Recurssion-- choose or no choose +//TC: 2^(O(n)) +class Solution { + public int rob(int[] nums) { + return helper(nums,0); + + } + + private int helper (int[] nums, int idx){ + //base case + if(idx>=nums.length) return 0; + + //logic + //no choose + int case1=helper(nums,idx+1); + //choose + int case2=nums[idx]+helper(nums,idx+2); + + return Math.max(case1,case2); + + } +} + +//Way:2 +//Recurssion-- choose or no choose -- causing time limit exceeded +//So, going with memoization-- remembering the result +//TC: O(n) +class Solution { + public int rob(int[] nums) { + int[] memo = new int[nums.length]; + return helper(nums,0,memo); + + } + + private int helper (int[] nums, int idx, int[] memo){ + //base case + if(idx>=nums.length) return 0; + + if(memo[idx]!=0) return memo[idx]; + + //logic + //no choose + int case1=helper(nums,idx+1,memo); + //choose + int case2=nums[idx]+helper(nums,idx+2,memo); + memo[idx]=Math.max(case1,case2); + + return Math.max(case1,case2); + + } +} + +//Way:3 +//Recurssion-- choose or no choose -- causing time limit exceeded +//So, going with memoization-- remembering the result +//TC: O(n) +class Solution { + public int rob(int[] nums) { + int[] memo = new int[nums.length]; + //TO avoid time limit exceeded for the input with all elements as 0 + for(int i=0;i=nums.length) return 0; + + if(memo[idx]!=Integer.MIN_VALUE) return memo[idx]; + + //logic + //no choose + int case1=helper(nums,idx+1,memo); + //choose + int case2=nums[idx]+helper(nums,idx+2,memo); + memo[idx]=Math.max(case1,case2); + + return Math.max(case1,case2); + + } +} + +//Way4: +//DP: tabulation +//We are choosing alternate houses. We can have one dp array which holds maximum value at that particular index +//At every index we have two options. To choose the house or not to choose the house +//Choose the house: sum of curr house and max value at (curr house -2) +//Not to choose the house: Max value at prev house i.e., currhouse-1 +//Choose max value from above steps +//TC: O(n); SC: O(n) +class Solution { + public int rob(int[] nums) { + int n=nums.length; + if(n==1) return nums[0]; + int[] dp=new int[n]; + dp[0]=nums[0]; + dp[1]=Math.max(nums[0],nums[1]); + + for(int i=2;i Date: Sun, 4 Jan 2026 13:14:49 -0600 Subject: [PATCH 2/2] Implement multiple approaches for coin change problem --- Leetcode_322.java | 137 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 Leetcode_322.java diff --git a/Leetcode_322.java b/Leetcode_322.java new file mode 100644 index 00000000..b9ef1156 --- /dev/null +++ b/Leetcode_322.java @@ -0,0 +1,137 @@ +//Way1 +//Exhaustive - choose and no choose +//TC: 2^(m+n); SC: stack space +class Solution { + public int coinChange(int[] coins, int amount) { + int ans= helper(coins,amount,0); + if(ans==Integer.MAX_VALUE-10) return -1; + return ans; + + } + + private int helper(int[] coins, int amount, int idx){ + //base case + if(idx==coins.length || amount<0) return Integer.MAX_VALUE-10; + + if(amount==0) return 0; + + //logic: + //no choose + int case1 = helper(coins,amount,idx+1); + + //choose + int case2=1+helper(coins,amount-coins[idx],idx); + + return Math.min(case1,case2); + + + } +} + + +//Way2: +//When we have opted for choose and no choose case, we got many repeated sub problems. So, we can choose dp to solve it. +//In dp, going with bottom up i.e., tabulation method. +//TC: O(m*n); SC: O(m*n) +class Solution { + public int coinChange(int[] coins, int amount) { + int m=coins.length; + int n=amount; + int[][] dp=new int[m+1][n+1]; + + //Number of coins required to make amount 0 with coins 0 + dp[0][0]=0; + //Number of coins required to make amount n with coins 0 + for(int i=1;i<=n;i++){ + dp[0][i]=Integer.MAX_VALUE-10; + } + + for(int i=1;i<=m;i++){ + for(int j=0;j<=n;j++){ + //Using previous rows to get current row. + if(j