From ba566e347a62097ec10de55e1ebc3fbbe69cfdd4 Mon Sep 17 00:00:00 2001 From: Sanjoli Date: Sun, 14 Jun 2026 14:30:43 -0500 Subject: [PATCH 1/2] copleted competitive coding 2 --- Problem1.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Problem1.java diff --git a/Problem1.java b/Problem1.java deleted file mode 100644 index e69de29b..00000000 From 0a788ca5683762279ee09e4b215179f90adbbd75 Mon Sep 17 00:00:00 2001 From: Sanjoli Date: Sun, 14 Jun 2026 14:34:07 -0500 Subject: [PATCH 2/2] copleted competitive coding 2 --- Problem1.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Problem1.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..61fb2dc2 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,36 @@ +import java.util.Arrays; + +public class Problem1 { + public static void main(String[] args) { + int[] val = {1, 2, 3}; + int[] wt = {4, 5, 1}; + int maxWt = 4; + int[][] memo = new int[val.length][maxWt + 1]; + for (int[] row : memo) { + Arrays.fill(row, -1); + } + System.out.println(recur(val, wt, maxWt, 0, 0, memo)); + } + + private static int recur(int[] val, int[] wt, int maxWt, int index, int totalWt, int[][] memo) { + if (index == val.length || totalWt == maxWt) { + return 0; + } + + if (memo[index][totalWt] != -1) { + return memo[index][totalWt]; + } + + if (totalWt + wt[index] > maxWt) { + memo[index][totalWt] = recur(val, wt, maxWt, index + 1, totalWt, memo); + return memo[index][totalWt]; + } + + memo[index][totalWt] = Math.max( + recur(val, wt, maxWt, index + 1, totalWt, memo), + recur(val, wt, maxWt, index + 1, totalWt + wt[index], memo) + val[index] + ); + + return memo[index][totalWt]; + } +} \ No newline at end of file