Grokking Dynamic Programming Patterns for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
DP / recursive solution

Mohammed Dh Abbas

Oct 13, 2024

class Solution: def canPartition(self, num): def solve(left, right, index, memo): # return from memo if index in memo and (str(left) + str(right)) in memo[index]: return memo[index][(str(left) + str(right))] # we reached the end if index == len(num): return False # we find equality if left == right: return True left_without = left - num[index] right_with = right + num[index] result = solve(left, right, index + 1, memo) or solve(left_without, right_with, index + 1, memo) if index not in memo: memo[index] = {} memo[index][(str(left) + str(right))] = result return result return solve(sum(num), 0, 0, {})

0

0

Comments
Comments

On this page

Problem Statement

Try it yourself