<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:
        """
        neetcode solution + memoization
        https://www.youtube.com/watch?v=mBk4I0X46oI
        """
        # basic condition (is it divisible?)
        # 3.33333 is not a valid number of groups
        # should be an integer.
        # If the total sum is not divisible by k, 
        # we can't make subsets.
        if sum(nums) % k != 0:
            return False
        
        # group sum to target for each group
        target = sum(nums) // k

        # to track if the num is used or not
        taken = ['0'] * len(nums)
        
        # sorting the array in descending order
        # so if first value is greater than target, 
        # it will not be included in any subset
        # so we cant partition the entire array into 
        # k equal sum subsets
        nums.sort(reverse = True)
        if nums[0] > target:
            return False

        # backtracking
        memo = {}
        def backtrack(i, k, subset_sum):
            """
            i = which index are we at in nums currently?
            k = how many k buckets we need to do now? k = 4, 3, 2, ...
            subset_sum = the current subset sum
            """
            taken_str = ''.join(taken)

            # base case
            if k == 0:
                return True   ## it is possible and we've done
            
            # if we have already computed the current combination
            if taken_str in memo:
                return memo[taken_str]

            # base case
            if subset_sum == target:
                # we are done for this subset we are currently at.
                # i = 0 because we start again for the next k-1
                # k-1 because we are moving on to the next one
                # subset_sum = 0 because we start again for the next k-1
                memo[taken_str] = backtrack(0, k-1, 0)
                return memo[taken_str]

            # recursive call
            for j in range(i, len(nums)):

                # either we include or exclude
                # but, we will include this num iff available
                # also, we will include this num iff it's not going over
                # the target
                # if either of these conditions true then we do not
                # want to proceed with 'including this num'.
                if taken[j] == '1' or subset_sum + nums[j] > target:
                    # we cannot include
                    continue
                
                # we can include
                taken[j] = '1'
                
                # we are currently at index j so we pass j+1
                # (the next index)
                # we included so subset_sum + nums[j]
                result = backtrack(j + 1, k, subset_sum + nums[j])
                
                if result:
                    return True 

                taken[j] = '0'   # reverse the decision

            # We were not able to make a valid combination after picking 
            # each element from the array, hence we can't make k subsets.
            memo[taken_str] = False
            return memo[taken_str]    # we could not find the target

        return backtrack(0, k, 0)
32803