-
Notifications
You must be signed in to change notification settings - Fork 12
/
276 Paint Fence.cpp
83 lines (65 loc) · 1.71 KB
/
276 Paint Fence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Link: https://leetcode.com/problems/paint-fence/
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.
Example 1:
Input: n = 3, k = 2
Output: 6
Explanation: All the possibilities are shown.
Note that painting all the posts red or all the posts green is invalid because there can only be at most one pair of adjacent posts that are the same color.
Example 2:
Input: n = 1, k = 1
Output: 1
Example 3:
Input: n = 7, k = 2
Output: 42
*/
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
// Milon
class Solution {
public:
int solve(int ind) {
if (ind == 1) {
return numOfCol;
} else if (ind == 2) {
return numOfCol * numOfCol;
}
int &ret = dp[ind];
if (ret != -1) {
return ret;
}
ret = 0;
ret = solve(ind - 1) * (numOfCol - 1);
ret += solve(ind - 2) * (numOfCol - 1);
return ret;
}
int numWays(int n, int k) {
numOfCol = k;
if (n == 1) {
return k;
}
dp.clear();
dp.resize(3, 0);
dp[0] = k;
dp[1] = k*k;
for (int i = 2; i < n; i++) {
dp[i%3] = dp[(i-1)%3] * (k - 1);
dp[i%3] += dp[(i-2)%3] * (k - 1);
}
return dp[(n-1)%3];
}
private:
int numOfCol;
vector<int> dp;
};
int main() {
Solution sol = Solution();
cout << sol.numWays(3, 2) << endl;
cout << sol.numWays(7, 2) << endl;
return 0;
}