<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>

from math import inf
from collections import deque
from bisect import bisect_left

class Solution:
    def canPartition(self, nums: List[int]) -> bool:
        """
        - Can smaller arrays be partitioned such that it scales?
        - Running sums could be
        - If you can make half with any combination of numbers, you have an answer

        11,5,5,1
        [11,5]
        [11,10]
        [11,11]

        4,3,3,2,2
        [4]
        [4,3]
        [7,3]

        1,2,3,4
        [1,2]
        [4,2],[1,5]

        - start w/largest num
        - 

        Bin search approach:
        - Find the target half value
        - Init counts = [0 for _ in range(101)]
            - counts[0] = 200
        - Iterate over nums, updating counts, and tracking the largest num
        - set curr = largest num, and decrement count
        - If curr > target, return False
        - 
        """
        if len(nums) < 2:
            return False
        elif len(nums) == 2:
            return nums[0] == nums[1]

        total = sum(nums)
        if total % 2 == 1:
            return False
        target = total // 2

        print(target)
        cache = {}
        res = self.backtrack(0, nums, target, cache)
        print(set(cache.values()))
        return res

    def backtrack(self, i: int, nums: List[int], target: int, cache: dict[tuple[int,int],bool]):
        if i == len(nums):
            return False
        elif nums[i] == target:
            return True
        elif (i, target) in cache:
            return cache[(i,target)]

        cache[(i,target)] = (
            self.backtrack(i+1, nums, target, cache)
            or self.backtrack(i+1, nums, target - nums[i], cache)
        )

        return cache[(i,target)]
275188