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.

解题思路:

首先我们要确定当前数组的和能分整除k,若不能整除k,则直接返回false。若能够整除,则可以进一步检查能否分成这些部分。

可以用dfs来解答。

需要用一个visited数组来记录是否已经用过这个数字。

用k来表示还剩多少部分, start 表示从哪里开始找,target表示目标和,cur_sum表示当前和。

代码:

DFS解法:

class Solution {
public:
    bool canPartitionKSubsets(vector<int>& nums, int k) {
        if(nums.empty()) return false;
        int sum = 0;
        for(int i : nums){
            sum += i;
        }
        if(sum % k != 0) return false;

        vector<bool> visited(nums.size(), false);
        return canPartition(nums, visited, 0, k, 0, sum/k);
    }

    bool canPartition(vector<int> &nums, vector<bool> &visited, int start, int k, int cur_sum, int target){
        if(k == 1) return true;
        if(cur_sum == target) return canPartition(nums, visited, 0, k-1, 0, target);
        for(int i = start; i < nums.size(); i++){
            if(!visited[i]){
                visited[i] = true;
                if(canPartition(nums,visited, i+1, k, cur_sum+nums[i], target)) return true;
                visited[i] = false;
            }
        }
        return false;
    }
};

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9485040.html

时间: 2024-08-30 13:31:20

698. Partition to K Equal Sum Subsets的相关文章

[Swift]LeetCode698. 划分为k个相等的子集 | 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

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

HDU 3280 Equal Sum Partitions(二分查找)

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

HDU-3280 Equal Sum Partitions

http://acm.hdu.edu.cn/showproblem.php?pid=3280 用了简单的枚举. Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 453    Accepted Submission(s): 337 Problem Description An equal sum

[LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be

6661 Equal Sum Sets(DP)

Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the number of se

UvaLive6661 Equal Sum Sets dfs或dp

UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cs

D。6661 - Equal Sum Sets

Equal Sum Sets Let us consider sets of positive integers less than or equal to n. Note that all elements of a set aredifferent. Also note that the order of elements doesnt matter, that is, both {3, 5, 9} and {5, 9, 3} meanthe same set.Specifying the

Aizu 1335 Equal Sum Sets

Description Let us consider sets of positive integers less than or equal to n. Note that all elements of a set are different. Also note that the order of elements doesn't matter, that is, both {3, 5, 9} and {5, 9, 3} mean the same set. Specifying the