276. (Locked)Paint Fence

Difficulty: Easy

Frequency: N/A

There is a fence with n posts, each post can be painted with one of the k colors.

You have to paint all the posts such that no more than two adjacent fence posts have the same color.

Return the total number of ways you can paint the fence.

Note: n and k are non-negative integers.


My solution:
Data structure:
Steps:
Complexity:
Runtime:
Space:
Test cases:
Corner cases:
Code:

class Solution {
public:
int numWays(int n, int k) {
if (n == 0) return 0;
if (n == 1) return k;
if (n == 2) return k * k;
int a = k, b = k * k, result;
for (int i = 3; i <= n; i++) {
result = (a + b) * (k – 1);
a = b;
b = result;
}
return result;
}
};


Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn:

Leave a comment