<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> if it is possible to divide this array into <code>k</code> non-empty subsets whose sums are all equal.</p>

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

<pre>
<strong>Input:</strong> nums = [4,3,2,3,5,2,1], k = 4
<strong>Output:</strong> true
<strong>Explanation:</strong> It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 3
<strong>Output:</strong> false
</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 16</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li>The frequency of each element is in the range <code>[1, 4]</code>.</li>
</ul>

class Solution:
    def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:

        # base case: can't partition evenly
        if sum(nums) % k:
            return False

        # get the target sum of each k subset
        target = sum(nums) // k
        

        # create our cache function
        cache = {}

        nums.sort()

        # create an array to mark the number at this index as used
        chosen = [0] * len(nums)

        def backtrack(i, count, currentSum):

            # we made k - 1 subsets with the target
            # the last subset also sums to target
            if count == k - 1:
                return True
            
            # our current sum is greater than target
            # return false
            if currentSum > target:
                return False
            
            # we have already calculated a result for the given choice of elements
            if tuple(chosen) in cache:
                return cache[tuple(chosen)]
            
            # subset is made when our current sum equals target
            if currentSum == target:
                # call the function from index 0 again
                # add 1 to our count of subsets
                # reset the current sum to 0
                cache[tuple(chosen)] = backtrack(0, count + 1, 0)
                return cache[tuple(chosen)]
            
            # iterate through non-picked elements
            for j in range(i, len(nums)):
                if not chosen[j]:
                    # mark this element as chosen
                    chosen[j] = 1
                    
                    # try to create valid elements from the next elements
                    if backtrack(j + 1, count, currentSum + nums[j]):
                        return True

                    # backtrack choosing this element
                    chosen[j] = 0
            
            # we were not able to make k subsets equal to target
            # return False
            cache[tuple(chosen)] = False
            return False
    
        # we start at index 0
        # we have 0 subsets created thus far
        # the sum of this current subset is 0
        return backtrack(0, 0, 0)
61179