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
17 changes: 17 additions & 0 deletions Knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Time Complexity : O(m * n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Use 1D array to keep track of max profit for every capacity.

class Solution:
def knapsack(self, W, val, wt):
n = W
m = len(wt)
dp = [0] * (n+1)

for i in range(m):
for j in range(n, wt[i] - 1, -1):
dp[j] = max(dp[j], val[i] + dp[j - wt[i]])

return dp[n]
17 changes: 17 additions & 0 deletions TwoSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Time Complexity : O(n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : If complement already in map then return the index else store complement.

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
map = dict()

for i, num in enumerate(nums):
if target-num in map:
return [i, map[target-num]]

map[num] = i

return [-1, -1]