<p>Given an integer array <code>nums</code>, return <code>true</code> <em>if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,5,11,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned as [1, 5, 5] and [11].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,5]
<strong>Output:</strong> false
<strong>Explanation:</strong> The array cannot be partitioned into equal sum subsets.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 200</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

class Solution:
    def canPartition1(self, nums: List[int]) -> bool:
        """
        total = sum(nums)
        if not total%2: return False
        dfs(i,curr)
        every level we decide if we include a number of not, subtract nums[i] from curr if include
        base condition: 
        if i == len(nums): return False
        if curr == 0: return True
        if we find one subset that makes up to total/2, we check the remaining of the array
        """
        def dfs(nums,i,curr):
            if curr == 0: # found subset that adds up to 
                return True
            if i == len(nums)-1 or curr < 0:
                return False
            #include, exclude, if one of them is true return true
            if dfs(nums,i+1,curr-nums[i+1]) or dfs(nums,i+1,curr):
                return True
            
        total = sum(nums)
        if total % 2 != 0:
            return False
        return dfs(nums,0,total//2)

    def canPartition(self, nums: List[int]) -> bool:
        """
        memo
        """
        def dfs(memo,nums,i,curr):
            if curr == 0: # found subset that adds up to 
                return True
            if i == len(nums)-1 or curr < 0:
                return False
            if (i,curr) in memo:
                return memo[(i,curr)]
            #include, exclude, if one of them is true return true
            result = dfs(memo,nums,i+1,curr-nums[i]) or dfs(memo,nums,i+1,curr)
            memo[(i,curr)] = result
            return memo[(i,curr)]
            
        total = sum(nums)
        if total % 2 != 0:
            return False
        memo = {}
        return dfs(memo,nums,0,total//2)

    def canPartition1(self, nums: List[int]) -> bool:
        total = sum(nums)
        #if odd return False
        if total & 1:
            return False
        
        subset_sum = total//2
        n = len(nums)
        
        dp = [False]*(subset_sum+1)
        dp[0] = True #base case condition always True, 0 element can make up 0 sum
        
        for i in range(n):
            for j in range(subset_sum,nums[i]-1,-1):
                dp[j] = dp[j] or dp[j-nums[i]]
        return dp[-1]
        
       
                    
            
        
        
        
        
144205