From df7978c691ced7297b4cae79036190a499855710 Mon Sep 17 00:00:00 2001 From: pratikb0501 Date: Sat, 6 Jun 2026 18:17:30 -0700 Subject: [PATCH] Completed CP-2 --- Problem1.java | 0 Problem1.py | 21 +++++++++++++++++++++ Problem2.java | 0 Problem2.py | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+) delete mode 100644 Problem1.java create mode 100644 Problem1.py delete mode 100644 Problem2.java create mode 100644 Problem2.py diff --git a/Problem1.java b/Problem1.java deleted file mode 100644 index e69de29b..00000000 diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..d3437bf1 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,21 @@ +def knapsack(W, val, wt): + + # Initializing dp list + dp = [0] * (W + 1) + + # Taking first i elements + for i in range(1, len(wt) + 1): + + # Starting from back, so that we also have data of + # previous computation of i-1 items + for j in range(W, wt[i - 1] - 1, -1): + dp[j] = max(dp[j], dp[j - wt[i - 1]] + val[i - 1]) + + return dp[W] + +if __name__ == "__main__": + val = [1, 2, 3] + wt = [4, 5, 1] + W = 4 + + print(knapsack(W, val, wt)) diff --git a/Problem2.java b/Problem2.java deleted file mode 100644 index e69de29b..00000000 diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..9926c837 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,19 @@ +# https://leetcode.com/problems/two-sum/description/ + +class Solution { + public int[] twoSum(int[] nums, int target) { + int n = nums.length; + int[] ans = new int[2]; + HashMap map = new HashMap<>(); + for (int i = 0; i < n; i++) { + int curr = nums[i]; + if (map.containsKey(target - curr)) { + ans[0] = i; + ans[1] = map.get(target - curr); + } else { + map.put(curr, i); + } + } + return ans; + } +} \ No newline at end of file