Skip to content

Latest commit

 

History

History
209 lines (173 loc) · 4.54 KB

File metadata and controls

209 lines (173 loc) · 4.54 KB

中文文档

Description

Given an integer array nums and an integer k, return true if it is 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 is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.

Example 2:

Input: nums = [1,2,3,4], k = 3
Output: false

 

Constraints:

  • 1 <= k <= nums.length <= 16
  • 1 <= nums[i] <= 104
  • The frequency of each element is in the range [1, 4].

Solutions

Python3

class Solution:
    def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
        s = sum(nums)
        target, m = divmod(s, k)
        if m != 0:
            return False

        cur = [0] * k
        n = len(nums)

        def dfs(i: int) -> bool:
            if i == n:
                return True
            for j in range(k):
                if j > 0 and cur[j - 1] == cur[j]:
                    continue
                cur[j] += nums[i]
                if cur[j] <= target and dfs(i + 1):
                    return True
                cur[j] -= nums[i]
            return False

        nums.sort(reverse=True)
        return dfs(0)
class Solution:
    def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
        @cache
        def dfs(state, t):
            if state == (1 << len(nums)) - 1:
                return True
            for i, v in enumerate(nums):
                if (state & (1 << i)):
                    continue
                if t + v > s:
                    break
                if dfs(state | (1 << i), (t + v) % s):
                    return True
            return False

        s, mod = divmod(sum(nums), k)
        nums.sort()
        if mod:
            return False
        return dfs(0, 0)

Java

class Solution {
	public boolean canPartitionKSubsets(int[] nums, int k) {
		int sum = (int) Arrays.stream(nums).sum();
		if (sum % k != 0) {
			return false;
		}

		Arrays.sort(nums);
		int low = 0, high = nums.length - 1;
		while (low < high) {
			int temp = nums[low];
			nums[low] = nums[high];
			nums[high] = temp;
			low++;
			high--;
		}
		return dfs(nums, new int[k], 0, sum / k);
	}

	private boolean dfs(int[] nums, int[] cur, int i, int target) {
		if (i == nums.length) {
			return true;
		}
		for (int j = 0; j < cur.length; j++) {
            if (j > 0 && cur[j - 1] == cur[j]) {
                continue;
            }
			cur[j] += nums[i];
			if (cur[j] <= target && dfs(nums, cur, i + 1, target)) {
				return true;
			}
			cur[j] -= nums[i];
		}
		return false;
	}
}

Go

func canPartitionKSubsets(nums []int, k int) bool {
	sum := 0
	for _, num := range nums {
		sum += num
	}
	if sum%k != 0 {
		return false
	}

	var (
		target = sum / k
		cur    = make([]int, k)
		n      = len(nums)
	)

	var dfs func(i int) bool
	dfs = func(i int) bool {
		if i == n {
			return true
		}
		for j := 0; j < k; j++ {
			if j > 0 && cur[j-1] == cur[j] {
				continue
			}
			cur[j] += nums[i]
			if cur[j] <= target && dfs(i+1) {
				return true
			}
			cur[j] -= nums[i]
		}
		return false
	}

	sort.Sort(sort.Reverse(sort.IntSlice(nums)))
	return dfs(0)
}

C++

class Solution {
public:
    bool canPartitionKSubsets(vector<int>& nums, int k) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if (sum % k != 0) return false;

        int target  = sum / k;
        int n = nums.size();
        vector<int> cur(k, 0);

        function<bool(int)> dfs;
        dfs = [&](int i) {
            if (i == n) return true;
            for (int j = 0; j < k; ++j) {
                if (j > 0 && cur[j - 1] == cur[j]) continue;
                cur[j] += nums[i];
                if (cur[j] <= target && dfs(i + 1)) return true;
                cur[j] -= nums[i];
            }
            return false;
        };

        sort(nums.begin(), nums.end(), greater<int>());
        return dfs(0);
    }
};

...