This problem is related to this and this one, but I want to put some restrictions here.
Repeat the question
So, I want to find out the number of possible ways to add 1, 2 and 3 to N. The solution can be calculated using recursive formula F[n] = F[n-1] + F[n-2] + F[n-3]
, where F[0] = 1
, F[1] = 1
, F[2] = 2
. Of course, using dynamic programming I can solve it in linear time.
My restriction
The restriction is: the resulting sequence must have no two elements repeated in a row.
So, for N = 4
the result could be [[1, 1, 1, 1], [2, 1, 1], [1, 2, 1], [3, 1], [1, 1, 2], [2, 2], [1, 3]]
, but 1 1 1 1
, 2 1 1
, 1 1 2
and 2 2
is forbidden, so, applying the restriction F[4]
becomes 3
.
Can someone say how the recursive formula for this problem will look like? Or maybe someone has better idea?
I'm posting this problem here, because it's related to dynamic programming, not to math and combinatorics.