Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
class Solution:
     def canPartitionKSubsets(self, nums, k):
         """
         :type nums: List[int]
         :type k: int
         :rtype: bool
         """
         total = sum(nums)
         if total % k != 0:
             return False
         subsetTotal = total // k
         visited = [0] * len(nums)
         return self.helper(k, 0, 0, visited, nums, k, subsetTotal)
     
     def helper(self, remainingSets, index, s, visited, nums, k, subsetTotal):
         if remainingSets == 1:
             return True
         if s == subsetTotal:
             return self.helper(remainingSets - 1, 0, 0, visited, nums, k, subsetTotal)
         for i in range(index, len(nums)):
             if visited[i] == 0 and s + nums[i] <= subsetTotal:
                 visited[i] = 1
                 if self.helper(remainingSets, i + 1, s + nums[i], visited, nums, k, subsetTotal):
                     return True
                 visited[i] = 0
         return False
class Solution:
     def canPartitionKSubsets(self, nums, k):
         """
         :type nums: List[int]
         :type k: int
         :rtype: bool
         """
         total = sum(nums)
         if total % k != 0:
             return False
         subsetTotal = total // k
         visited = [0] * len(nums)
         return self.helper(k, 0, 0, visited, nums, k, subsetTotal)
     
     def helper(self, remainingSets, index, s, visited, nums, k, subsetTotal):
         if remainingSets == 1:
             return True
         if s == subsetTotal:
             return self.helper(remainingSets - 1, 0, 0, visited, nums, k, subsetTotal)
         for i in range(index, len(nums)):
             if visited[i] == 0 and s + nums[i] <= subsetTotal:
                 visited[i] = 1
                 if self.helper(remainingSets, i + 1, s + nums[i], visited, nums, k, subsetTotal):
                     return True
                 visited[i] = 0
         return False