dp
generating functions
]
Leetcode 0377. Combination Sum IV
Problem statement
https://leetcode.com/problems/combination-sum-iv/
Solution
You probably saw a lot of different solutions for this problem, but I can guess, that you did not saw this one. The idea is here is to use generating functions. Imagine, that we have coins 1, 2, 5
. Let us create polynomial (x^1 + x^2 + x^5)
. Now, let us consider powers of this polynom. For example:
(x^1 + x^2 + x^5)^ 1 = (x^1 + x^2 + x^5)
, nothing very intersting here.(x^1 + x^2 + x^5)^2 = x^2 + x^3 + x^6 + x^3 + x^4 + x^8 + x^6 + x^7 + x^10
. What we have here. For example we can see that there is only one way to get target2
, because coefficient will be equal to1
beforex^2
, there is2
ways to get6
and so on.- In general if we have
(x^1 + x^2 + x^5)^k
, then coefficients will show us how many ways we can get one or another target, using exactly k coins.
Now, we can use problem restrictions: that is that answer will always be less than 2^32
, so if we use x = 2^32
and will work with operations in 2^32
base system, everything will be fine. Also we use python long numbers, which is very helpful here.
- Create
T
is our polynomial withx = 2^32
. - Define
S = 1
andans = 0
. - Iterate over powers of
T
. Each time we multiplyS
byT
. However we do not want to have too big numbers, so we use& (1<<(32*t+32)) - 1
, trick, which will give us lastt + 1
digits in2^32
base system. - Update our answer: this is value
S >> (32*t)
, that is value oft
-th digit in our2^32
base system.
Complexity
It is a bit difficult to estimate it like this, but on each operation and we have t
of them we will work with number which have no more than O(32m)
bits, where m
is the biggest number among nums.
So, total time complexity will be O(32*m*t)
, space complexity is O(32*m)
. However in practice it works quite fast.
Code
class Solution:
def combinationSum4(self, nums, t):
T = sum([1<<(32*n) for n in nums])
S, ans = 1, 0
for i in range(t):
S = (S*T) & (1<<(32*t+32)) - 1
ans += S >> (32*t)
return ans