698. Partition to K Equal Sum Subsets

698. Partition to K Equal Sum Subsets

  • 方法1: backtracking
    • Complexity

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. 1 <= k <= len(nums) <= 16.
  2. 0 < nums[i] < 10000.

方法1: backtracking

思路:

虽然是由2 变成k,但是画风突变,不能用dynamic programming 做了,变成了一道backtracking的好题。首先我们检查sum能不能被k整除,不能直接返回false。在helper中,记录一个bool vector: visited,记录每一个数字是否被使用过。k此时代表还有多少组subset要搜索,start代表本次搜索开始的位置,curSum表示当前累加和。终止条件由很多种,首先,如果k = 0, 表示已经找到了k组cursum == target, 返回true(其实k = 1 已经可以返回true了,但速度没什么变化)。如果cursum == sum, 说明又找到一组subset,继续递归找k - 1组。

Complexity

Time complexity: O(n^k) ??
Space complexity: O(n)

class Solution {
public:
    bool canPartitionKSubsets(vector<int>& nums, int k) {
        int n = nums.size();
        vector<bool> visited(n, false);
        int target = accumulate(nums.begin(), nums.end(), 0);
        if (target % k) return false;
        target /= k;
        return partitionHelper(nums, visited, k, target, 0, 0);
    }
    
    bool partitionHelper(vector<int> & nums, vector<bool> & visited, int k, int target, int curSum, int start) {
        if (k == 0) return true;
        if (curSum == target) return partitionHelper(nums, visited, k - 1, target, 0, 0);
        
        for (int i = start; i < nums.size(); i++) {
            if (!visited[i]) {
                visited[i] = true;
                if (partitionHelper(nums, visited, k, target, curSum + nums[i], i + 1))
                    return true;;
                visited[i] = false;
            }
        }
        return false;
    }
};

剪枝:先给数组按从大到小的顺序排个序,然后在递归函数中,可以直接判断,如果curSum大于target了,直接返回false。因为题目中限定了都是正数,并且我们也给数组排序了,后面的数字只能更大,可以提速很多。

class Solution {
public:
    bool canPartitionKSubsets(vector<int>& nums, int k) {
        int n = nums.size();
        vector<bool> visited(n, false);
        int target = accumulate(nums.begin(), nums.end(), 0);
        if (target % k) return false;
        target /= k;
        sort(nums.begin(), nums.end(), greater<int>());
        return partitionHelper(nums, visited, k, target, 0, 0);
    }
    
    bool partitionHelper(vector<int> & nums, vector<bool> & visited, int k, int target, int curSum, int start) {
        if (k == 1) return true;
        if (curSum > target) return false;
        if (curSum == target) return partitionHelper(nums, visited, k - 1, target, 0, 0);
        
        for (int i = start; i < nums.size(); i++) {
            if (!visited[i]) {
                visited[i] = true;
                if (partitionHelper(nums, visited, k, target, curSum + nums[i], i + 1))
                    return true;;
                visited[i] = false;
            }
        }
        return false;
    }
};

你可能感兴趣的:(698. Partition to K Equal Sum Subsets)