From c1017acf63b8074bd2dc0a2d82574217387f62e5 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sun, 4 Jan 2026 22:08:42 -0600 Subject: [PATCH 1/2] Add three methods to calculate minimum painting cost Implemented three approaches to solve the minimum cost problem: recursion, memoization, and tabulation. --- Leetcode_256.java | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Leetcode_256.java diff --git a/Leetcode_256.java b/Leetcode_256.java new file mode 100644 index 00000000..40b0e012 --- /dev/null +++ b/Leetcode_256.java @@ -0,0 +1,107 @@ +//way1 +//Let's try to explore all the flows. And identify the minimum possible route +//Tc: m*(2^n) +class Solution { + public int minCost(int[][] costs) { + int case1=helper(costs,0,0); + + int case2=helper(costs,0,1); + + int case3=helper(costs,0,2); + + return Math.min(case1,Math.min(case2,case3)); + + } + + private int helper(int[][] costs, int i, int j){ + //base case + if(i==costs.length) return 0; + + + //logic + if(j==0){ + return costs[i][j] + Math.min(helper(costs, i+1, 1), + helper(costs,i+1,2)); + }else if(j==1){ + return Math.min(costs[i][j]+helper(costs,i+1,0), + costs[i][j]+helper(costs,i+1,2)); + }else{ + return Math.min(costs[i][j]+helper(costs,i+1,0), + costs[i][j]+helper(costs,i+1,1)); + } + } +} + +//way2 +//Let's try to explore all the flows. And identify the minimum possible route -- causing time limit exceeded +//So, doing memoization. Trying to remember prev results +//Tc: m*n; SC: m*n +class Solution { + public int minCost(int[][] costs) { + + int[][] memo=new int[costs.length][3]; + int case1=helper(costs,0,0,memo); + + int case2=helper(costs,0,1,memo); + + int case3=helper(costs,0,2,memo); + + return Math.min(case1,Math.min(case2,case3)); + + } + + private int helper(int[][] costs, int i, int j,int[][] memo){ + //base case + if(i==costs.length) return 0; + + if(memo[i][j]!=0) return memo[i][j]; + + + //logic + if(j==0){ + memo[i][j]= costs[i][j] + Math.min(helper(costs, i+1, 1,memo), + helper(costs,i+1,2,memo)); + return memo[i][j]; + }else if(j==1){ + memo[i][j] = Math.min(costs[i][j]+helper(costs,i+1,0,memo), + costs[i][j]+helper(costs,i+1,2,memo)); + return memo[i][j]; + }else{ + memo[i][j] = Math.min(costs[i][j]+helper(costs,i+1,0,memo), + costs[i][j]+helper(costs,i+1,1,memo)); + return memo[i][j]; + } + } +} + +//way3: +//Tabulation +//Tracking all the flows and identifying the min at every step +//Returning min at last row +//TC: O(m*n); SC: O(m*n) + +class Solution { + public int minCost(int[][] costs) { + int n=costs.length; + + int[][] dp=new int[n][3]; + dp[0][0]=costs[0][0]; + dp[0][1]=costs[0][1]; + dp[0][2]=costs[0][2]; + + for(int i=1;i Date: Sun, 4 Jan 2026 22:10:42 -0600 Subject: [PATCH 2/2] Add multiple solutions for coin change problem Implemented four different approaches to solve the coin change problem, including exhaustive search, memoization, and tabulation methods. --- Leetcode_518.java | 119 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 Leetcode_518.java diff --git a/Leetcode_518.java b/Leetcode_518.java new file mode 100644 index 00000000..33b6b8d6 --- /dev/null +++ b/Leetcode_518.java @@ -0,0 +1,119 @@ +//way1 +//Exhaustive-- choose all the combinations. If the combinations is equals to target, add it to result +//TC: 2^(m+n) +class Solution { + public int change(int amount, int[] coins) { + return helper(amount,coins,0); + + } + private int helper(int amount, int[] coins, int i){ + //base case + if(amount==0) return 1; + + if(amount<0 || i==coins.length){ + return 0; + } + + //logic + //no choose + int case1=helper(amount,coins,i+1); + + int case2=helper(amount-coins[i], coins, i); + + return case1+case2; + } +} + +//way2: +//Exhaustive-- choose all the combinations. If the combinations is equals to target, add it to result -- causing time limit exceeded +//Memoization-- remembering prev results and using them. +//TC: O(m*n); SC: O(m*n) +class Solution { + public int change(int amount, int[] coins) { + int[][] memo=new int[coins.length+1][amount+1]; + for(int i=0; i<=coins.length; i++){ + Arrays.fill(memo[i], -1); + } + return helper(amount,coins,0,memo); + + } + private int helper(int amount, int[] coins, int i,int[][] memo){ + //base case + if(amount==0) return 1; + + if(amount<0 || i==coins.length){ + return 0; + } + + if(memo[i][amount]!=-1) return memo[i][amount]; + + //logic + //no choose + int case1=helper(amount,coins,i+1,memo); + + int case2=helper(amount-coins[i], coins, i,memo); + + memo[i][amount]=case1+case2; + + return case1+case2; + } +} + +//way3: +//Tabulation +//Identify number of combinations for amount 0 to target with the given coins +//return the last element of dp array +//tc: o(m*n); sc:o(m*n) + +class Solution { + public int change(int amount, int[] coins) { + int n=coins.length; + int m=amount; + + int[][] dp=new int[n+1][m+1]; + + dp[0][0]=1; + + for(int i=1;i<=n;i++){ + for(int j=0;j<=m;j++){ + if(j