From 1e929623290c5b0211aab06b9e722d0ea62a9288 Mon Sep 17 00:00:00 2001 From: Tanmay Nalawade Date: Mon, 20 Oct 2025 10:07:49 -0700 Subject: [PATCH 1/2] Two sum complete --- 1. Two Sum.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1. Two Sum.py diff --git a/1. Two Sum.py b/1. Two Sum.py new file mode 100644 index 00000000..91bffa76 --- /dev/null +++ b/1. Two Sum.py @@ -0,0 +1,16 @@ +# Time: O(n) +# Space O(n) + +# first loop through the array and add all the elements in the hash_map +# then loop through the nums array one more time and check if the difference between the target and the element is there in the map +# if yes then return the ith index and the index associated with the found element in the map +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + my_map = {} + + for i in range(len(nums)): + my_map[nums[i]] = i + for i in range(len(nums)): + diff = target - nums[i] + if diff in my_map and my_map[diff] != i: + return [i,my_map[diff]] \ No newline at end of file From ed057d9437a898bc80671c84c804d245fdc4db09 Mon Sep 17 00:00:00 2001 From: Tanmay Nalawade Date: Wed, 22 Oct 2025 20:11:35 -0700 Subject: [PATCH 2/2] 0/1 Knapsack complete --- 0-1 Knapsack Problem.py | 36 ++++++++++++++++++++++++++++++++++++ 1. Two Sum.py | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 0-1 Knapsack Problem.py diff --git a/0-1 Knapsack Problem.py b/0-1 Knapsack Problem.py new file mode 100644 index 00000000..636386cf --- /dev/null +++ b/0-1 Knapsack Problem.py @@ -0,0 +1,36 @@ +# Time: O(2^n) in worst case each call spawns 2 recursive calls +# Space: O(n) recursion is almost n calls deep +class Solution: + def knapSack(self, W: int, weights: List[int], profits: List[int]) -> int: + return self.helper(W, weights, profits, 0) + def helper(self, w, weights, profits, idx): + # base + if idx >= len(weights) or w <= 0: + return 0 + if weights[idx] > w: + return self.helper(w, weights, profits, idx + 1) + + # not choose + not_choose = self.helper(w, weights, profits, idx + 1) + # choose + choose = profits[idx] + self.helper(w - weights[idx],weights, profits, idx +1) + + return max(choose,not_choose) + + +# TABULATION APPROACH +# Time and Space:O(m*n) m is the max capacity and n is the number of elements in weights array +# If the current capacity is less than the weights then just copy the previous row (not_choose) +# If not then the current element is maximum between the previous element(not_choose) and profits at curr( ie. choose) + the value required to make the remaining amount which will be in the previous row +class Solution: + def knapSack(self, W: int, weights: List[int], profits: List[int]) -> int: + dp_matrix = [[0 for _ in range(W + 1)]for _ in range(len(weights) + 1)] + + for i in range(1,len(dp_matrix)): + for j in range(1,len(dp_matrix[0])): + if j < weights[i-1]: + dp_matrix[i][j] = dp_matrix[i-1][j] + else: + dp_matrix[i][j] = max(dp_matrix[i-1][j],profits[i-1] + dp_matrix[i-1][j - weights[i-1]]) + + return dp_matrix[len(dp_matrix)-1][len(dp_matrix[0])-1] \ No newline at end of file diff --git a/1. Two Sum.py b/1. Two Sum.py index 91bffa76..2a08c3a0 100644 --- a/1. Two Sum.py +++ b/1. Two Sum.py @@ -3,7 +3,7 @@ # first loop through the array and add all the elements in the hash_map # then loop through the nums array one more time and check if the difference between the target and the element is there in the map -# if yes then return the ith index and the index associated with the found element in the map +# if yes then return the ith index and the index associated with the found element in the class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: my_map = {}