<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:
        # 1 solution:
        total = sum(nums)
        if total % k:
            return False
        nums.sort(reverse = True)
        reqSum = total // k
        used = [False] * len(nums)
        memo = {}
        def backtrack(subset, k):
            if k == 0:
                return True
            if subset == reqSum:
                return backtrack(0, k-1)
            key = (subset, k, tuple(used))
            if key in memo:
                return memo[key]
            for j in range(len(nums)):
                if used[j] or subset + nums[j] > reqSum:
                    continue
                used[j] = True
                if backtrack(subset + nums[j], k):
                    memo[key] = True
                    return True
                used[j] = False
                while j + 1 < len(nums) and nums[j + 1] == nums[j]:
                    j += 1
            
            memo[key] = False
            return False

        return backtrack(0, k) 
        # total = sum(nums)
        # if total % k:
        #     return False
        # nums.sort(reverse = True)
        # reqSum = total // k
        # used = [False] * len(nums)
        
        # def backtrack(index, subset, k):
        #     if k == 0 :
        #         return True
        #     if subset == reqSum:
        #         return backtrack(0, 0, k-1)
        #     for j in range(index, len(nums)):
        #         if used[j] or subset + nums[j] > reqSum:
        #             continue
        #         used[j] = True
        #         if backtrack(j + 1, subset + nums[j], k):
        #             return True
        #         used[j] = False
        #         while j + 1 < len(nums) and nums[j + 1] == nums[j]:
        #             j += 1
        #         if not subset:
        #             return False
            
        #     return False

        # return backtrack(0, 0, k)       

        # 2nd solution:
        # total = sum(nums) ## the sum of nums
        # if total % k: return False ## if the sum of nums in divisible by k return false
        # nums.sort(reverse = True)
        # reqSum = total // k 
        # subsets = [0] * k
        
        # def backtrack(index):
        #     if index == len(nums):
        #         return True
        #     for j in range(k):
        #         if subsets[j] + nums[index] <= reqSum:
        #             subsets[j] += nums[index]
        #             if backtrack(index + 1):
        #                 return True
        #             subsets[j] -= nums[index]
        #             if subsets[j] == 0:
        #                 break
        #     return False

        # return backtrack(0)
       
35821