Skip to content

Instantly share code, notes, and snippets.

def solve_knapsack(profits, weights, capacity):
dp = [[-1 for _ in range(capacity+1)] for _ in range(len(profits))]
return solve_knapsack_recursive(dp, profits, weights, capacity, 0)
def solve_knapsack_recursive(dp, profits, weights, capacity, currentIndex):
n = len(profits)
# base checks
if capacity <= 0 or n == 0 or len(weights) != n or currentIndex >= n:
return 0
def solve_knapsack(profits, weights, capacity):
return solve_knapsack_recursive(profits, weights, capacity, 0)
def solve_knapsack_recursive(profits, weights, capacity, currentIndex):
n = len(profits)
# base checks
if capacity <= 0 or n == 0 or len(weights) != n or currentIndex >= n:
return 0
def findTargetSumWays(nums: List[int], S: int) -> int:
totalSum = sum(nums)
if totalSum < S or (S + totalSum) % 2 == 1:
return 0
n = len(nums)
s_total = int((S+totalSum)/2)
dp = [0 for _ in range(s_total+1)]
dp[0] = 1
for num in nums:
def count_subsets(num, sum):
n = len(num)
dp = [[-1 for x in range(sum+1)] for y in range(n)]
# populate the sum = 0 columns, as we will always have an empty set for zero sum
for i in range(0, n):
dp[i][0] = 1
# with only one number, we can form a subset only when the required sum is
# equal to its value
def count_subsets(num, sum):
# create a two dimensional array for Memoization, each element is initialized to '-1'
dp = [[-1 for x in range(sum+1)] for y in range(len(num))]
return count_subsets_recursive(dp, num, sum, 0)
def count_subsets_recursive(dp, num, sum, currentIndex):
# base checks
if sum == 0:
return 1
def can_partition(num):
s = sum(num)
dp = [[-1 for x in range(s+1)] for y in range(len(num))]
return can_partition_recursive(dp, num, 0, 0, 0)
def can_parition_recursive(dp, num, currentIndex, sum1, sum2):
# base check
if currentIndex == len(num):
return abs(sum1 - sum2)
def can_partition(num):
s = sum(num)
n = len(num)
dp = [[False for x in range(int(s/2)+1)] for y in range(n)]
# populate the s=0 columns, as we can always form '0' sum with an empty set
for i in range(0, n):
dp[i][0] = True
# with only one number, we can form a subset only when the required sum is equal to that number
for each number 'i'
add number 'i' to S1 and recursively process the remaining numbers
add number 'i' to S2 and recursively process the remaining numbers
return the minimum absolute difference of the above two sets
def can_partition(num):
return can_partition_recursive(num, 0, 0, 0)
def can_partition_recursive(num, currentIndex, sum1, sum2):
def can_partition(num, sum):
n = len(num)
dp = [[False for x in range(sum+1)] for y in range(n)]
# populate the sum = 0 columns, as we can always form '0' sum with an empty set
for i in range(0, n):
dp[i][0] = True
# with only one number, we can form a subset only when the required sum is
# equal to its value
for each number 'i'
create a new set which INCLUDES number 'i' if it does not exceed 'S', and recursively
process the remaining numbers
create a new set WITHOUT number 'i', and recursively process the remaining numbers
return true if any of the above two sets has a sum equal to 'S', otherwise return false