From d7b3060a2323db838aa23e27e734dfaa49558bd9 Mon Sep 17 00:00:00 2001 From: pranjay01 Date: Sun, 1 Mar 2026 17:39:14 -0800 Subject: [PATCH] Done --- Problem1.py | 29 +++++++++++++++++++++++++++++ Problem2.py | 19 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..e368244d --- /dev/null +++ b/Problem1.py @@ -0,0 +1,29 @@ +#Given two arrays, val[] and wt[], where each element represents the value +# and weight of an item respectively, and an integer W representing the maximum +# capacity of the knapsack (the total weight it can hold). + + +def solution(val, wt, W): + # create 2 arrays 1 for updating current max values, other for using previous rows max values + # initialize of length w with base case where if no weight then max value of achieve that wight + maxValArrCurr = [0]*(W+1) + maxValArrPrev = [0]*(W+1) + for i in range(0,len(wt)): + currentWt = wt[i] + currentVal = val[i] + for j in range(0,len(maxValArrCurr)): + if j < currentWt: + maxValArrCurr[j] = maxValArrPrev[j] + continue + #noChoose value coming from top + noChoose = maxValArrPrev[j] + #choose value coming from chosing the wt and then fetching the value + choose = currentVal + if j - currentWt >=0: + choose += maxValArrPrev[j - currentWt] + maxValArrCurr[j] = max(noChoose,choose) + maxValArrPrev = list(maxValArrCurr) + + return maxValArrCurr[len(maxValArrCurr)-1] +print(solution([6,10,12], [1,2,3], 5 )) + diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..b5c5d404 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,19 @@ +#Two Sum + +#Time complexity -> On +#Space complexity -> On +#Logic -> check if the compliment (target-current num) if the num we are iterating on is present in dict. If present they both can make the target so return the index + + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + numToIndex = {} + for i in range (0, len(nums)): + num = nums[i] + complement = target - num + if complement in numToIndex: + return [numToIndex[complement], i] + + numToIndex[num] = i + + return -1