Last updated
Was this helpful?
Last updated
Was this helpful?
Given an array of integersnums
and a positive integerk
, find whether it's possible to divide this array intok
non-empty subsets whose sums are all equal.
Example 1:
Note:
1 <= k <= len(nums) <= 16
.
0 < nums[i] < 10000
.
这题好像有比较fancy的solution,感觉是记不住的。这里我只用了深搜,很暴力地搜。这题感觉像是的follow up。前一题能用dp,这题...感觉还是深搜比较好理解。
至于复杂度嘛...have no idea。感觉有点像。粗略估算,然后因为要找k个subset,所以起码k层,然后每一层的深度depends on target,如果target大的话,最坏情况可能有O(N),就是全部数字都加上才找到target。然后时间,就真难算,因为用了continue去排除一些一定不合法的选项。每次combination sum会用2^N,然后要求k个,所以O(K*2^n)