Leetcode 276 Solution

This article provides solution to leetcode question 276 (paint-fence)

https://leetcode.com/problems/paint-fence

Solution

class Solution {
public:
    int numWays(int n, int k) {
        if (n == 0 || k == 0)
            return 0;

        int diff = k;
        int same = 0;

        for (int i = 2; i <= n; i++)
        {
            int tmp = diff;
            diff = (same + diff) * (k - 1);
            same = tmp;
        }

        return same + diff;
    }
};