You are painting a fence of n posts with k different colors. You must paint the posts following these rules:

- Every post must be painted exactly one color.
- At most one pair of adjacent fence posts can have the same color.

Given the two integers n and k, return the number of ways you can paint the fence.

[Link]

The recursive solution times out! So let’s think of a dynamic programming solution!

**Single Fence**

opt(0) = k options

**Double Fence**

opt(1) = k^2 options

**Third Fence**

When first two fences are same color: k * (k-1) = opt(0) * (k-1)

When first two fences are diff color: k * (k-1) * k = k^2 * (k-1) = opt(1) * (k-1)

opt(2) = opt(0) * (k-1) + opt(1) * (k-1)

**Nth Fence??**

opt(i) = opt(i-2) * (k-1) + opt(i-1) * (k-1)

/** * @param {number} n * @param {number} k * @return {number} */ var numWays = function(n, k) { if (n===0) return 0; if (n===1) return k; const dp = new Array(n); dp[0] = k; dp[1] = k*k; for (let i = 2; i < n ; i++ ) { dp[i] = (dp[i-2] * (k-1)) + (dp[i-1] * (k-1)); } return dp[n-1]; }; |

The recursive solution (this will timeout).

/** * @param {number} n * @param {number} k * @return {number} */ var numWays = function(n, k) { if (n <= 2) return Math.pow(k, n); return countForOneColor([], 0, n, k); }; var countForOneColor = function(curr, level, n, k) { if (level === n) return 1; let ret = 0; for (let i = 0 ; i < k ; i++) { if (level >= 2 && curr[level-1] === curr[level-2] && i === curr[level-1]) continue; curr[level] = i; ret += countForOneColor(curr, level+1, n, k); curr.pop(); } return ret; } |

Helpful advice. Regards. https://definitionessays.com/ homework help line

Position well used.! https://definitionessays.com/ student dissertation

Nicely put, Cheers.

how to write an descriptive essay https://altertraff.com/ business writing service

Regards. Loads of tips.

going to college essay essay custom writing write my report

This is nicely said. !

medicine online order drugs online prescription drugs canada

You made the point.

essays about service https://quality-essays.com/ top ghostwriters

Nicely put. Regards!

custom order essays https://freshappshere.com/ help with writing dissertation

Incredible all kinds of good info!

how to write personal essays https://quality-essays.com/ academic writers

You said it nicely..

write that essay essay help with report writing

This is nicely said. !

online help with essay writing parents writing college essays professional cover letter writing service

Fantastic material, Kudos!

college essay titles https://altertraff.com/ copywriting services

Useful forum posts. With thanks.

website essay https://ouressays.com/ professional business letter writing services

Appreciate it, A good amount of content.

the perfect college essay essay writer creative writing services

Thanks a lot. Ample knowledge!

i need help writing an argumentative essay college essay writing services company

You said it adequately.!

college essay word count argumentative essay topics writing services company

Many thanks. A good amount of data!

college essay on diversity writing my essay rewriting services

With thanks, Ample material.

essay cheap https://flowleadsua.com/ ghostwriter service