Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'''
The solution is to find a matching number given to the current number.
Matching number for a current number is the one that produces the sum = target.

We keep a hashmap of the numbers visited before along with their index.
For the current number if we find the target - current number then we return
indexes of the numbers.

Time complexity: O(n)
Space complexity: O(n)
'''
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hMap = {}
for idx, i in enumerate(nums):
if target - i in hMap:
return [hMap[target-i], idx]
hMap[i] = idx
38 changes: 38 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
Brute force is to traverse a tree of the choice being choose or not choose
a given item until the bag is full or items can no longer be fit.
Time complexity: O(2^n)
Space complexity: O(n)

We can use the dp to optimize the repeated subproblems.
Using the given items calculate the maximum profit with capacity k.
Start the k from 0 to W.
Keep adding items until the entire grid is filled in the dp.

Choosing an item (i) means we are choosing max profit from the items before i
and capacity of w - w[i]. Then we add i to get final capacity of w and maximum
profit by adding i.

Hence the formula:
dp[j] = max(prev[j], valMap[w] + prev[j-w])
Time complexity = O(W * n)
Space complexity = O(W)
'''
class Solution:
def knapsack(self, W, val, wt):
# code here
valMap = {}
for i in range(len(wt)):
valMap[wt[i]] = val[i]

prev = [0 for _ in range(W+1)]
dp = [0 for _ in range(W+1)]
sortedWeights = sorted(wt)

for w in sortedWeights:
for j in range(w, W+1):
dp[j] = max(prev[j], valMap[w] + prev[j-w])

prev = dp

return dp[-1]