-
Notifications
You must be signed in to change notification settings - Fork 481
/
0698.cpp
41 lines (34 loc) · 1018 Bytes
/
0698.cpp
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
class Solution
{
public:
bool canPartitionKSubsets(vector<int>& nums, int k)
{
s = 0, n = nums.size();
for (int i : nums) s += i;
if (s % k) return false;
t = s / k, this->k = k;
vis = vector<int>(n);
sort(nums.begin(), nums.end(), greater<int>());
return dfs(nums, 0, 0, 0);
}
private:
int n, s, t, k;
vector<int> vis;
bool dfs(vector<int>& nums, int cur, int u, int p)
{
if (u == k) return true;
if (cur == t) return dfs(nums, 0, u + 1, 0);
for (int i = p, j; i < n; i++)
{
if (nums[i] + cur > t || vis[i]) continue;
vis[i] = 1;
if (dfs(nums, cur + nums[i], u, i + 1)) return true;
vis[i] = 0;
if (!cur || cur + nums[i] == t) return false;
j = i;
while (j < n && nums[i] == nums[j]) j++;
i = j - 1;
}
return false;
}
};