698. Partition to K Equal Sum Subsets

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

 

Approach #1: DFS + Backtracking. [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
    bool canPartitionKSubsets(vector<int>& nums, int k) {
        int len = nums.size();
        if (k == 1) return true;
        if (len < k) return false
         
        int sum = 0;
        for (int num : nums)
            sum += num;
        if (sum % k != 0) return false;
         
        int avg = sum / k;
        vector<int> token(len+5, 0), subsets(k+5, 0);
        subsets[0] = nums[len-1];
        token[len-1] = 1;
        return solve(nums, token, subsets, avg, k, len, 0, len-1);
    }
     
private:
    bool solve(vector<int>& nums, vector<int>& token, vector<int>& subsets,
               const int& avg, const int& k, const int& len, int curIdx, int limitIdx) {
        if (subsets[curIdx] == avg) {
            if (curIdx == k-2) return true;
            return solve(nums, token, subsets, avg, k, len, curIdx+1, len-1);
        }
         
        for (int i = limitIdx; i >= 0; --i) {
            if (token[i] == 1) continue;
            int tmp = subsets[curIdx] + nums[i];
             
            if (tmp <= avg) {
                subsets[curIdx] += nums[i];
                token[i] = 1;
                bool nxt = solve(nums, token, subsets, avg, k, len, curIdx, i-1);
                subsets[curIdx] -= nums[i];
                token[i] = 0;
                if (nxt) return true;
            }
        }
         
        return false;
    }
};

  

 

Analysis:

We can solve this problem recursively, we keep an array for sum of each partition and a array to check whether an element is already taken into some partition or not.

First we need to check some base cases:

If K is 1, then we already have our answer, complete array is only sbset with same sum.

If N < K, then it is not possible to divide array into subsets with equal sum, because we can't divide the array into more than N parts.

If sum of array is not divisible by K. then it is not possible to divide the array. We will proceed only if k divides sum. Our goal reduces to divide array into K parts where sum of each part should be array_sum / k

In above code  a recursive method is written which tries to add array element into some subset. If sum of this subset reaches required sum, we iterator for next part recursively, otherwise we backtrack for different set of elements. If number of subsets whose sum reaches the required sum is (K-1), we flag that it is possible to partition array nto K parts with equal sum, because remaining elements already have a sum equal to required sum.

 

Reference:

https://www.geeksforgeeks.org/partition-set-k-subsets-equal-sum/

 

posted @   Veritas_des_Liberty  阅读(231)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
历史上的今天:
2018-03-14 C - Trailing Zeroes (III)(二分)
2018-03-14 B - Pie (二分)
点击右上角即可分享
微信分享提示