Medium
Count the Number of K-Free Subsets — Python
Full explanation · Time O(n) · Space O(n)
# Time: O(n)
# Space: O(n)
import collections
import operator
# combinatorics, dp
class Solution(object):
def countTheNumOfKFreeSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
def count(x):
y = x
while y-k in cnt:
y -= k
dp = [1, 0] # dp[0]: count without i, dp[1]: count with i
for i in xrange(y, x+1, k):
dp = [dp[0]+dp[1], dp[0]*((1<<cnt[i])-1)]
return sum(dp)
cnt = collections.Counter(nums)
return reduce(operator.mul, (count(i) for i in cnt.iterkeys() if i+k not in cnt))