-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaintFence.java
46 lines (35 loc) · 1.13 KB
/
PaintFence.java
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
package techQuestions;
public class PaintFence {
public static void main(String[] args) {
// TODO Auto-generated method stub
int posts = 1, colors = 0;
System.out.println(paintFence(posts, colors));
}
public static int paintFence(int n, int k) {
// if there are no posts to color, then return 0 possibilities
if (n == 0) {
return 0;
}
// if there is just one post to color, then return number of colors
if (n == 1) {
return k;
}
// if there are no colors, there are no options
if (k == 0) {
return 0;
}
// initializing first two posts for the count
// there are as many options as there are colors to paint first two the same
int sameColorCount = k;
// there are this many options to paint first two different colors
int diffColorCount = k * (k - 1);
for (int i = 2; i < n; i++) {
int temp = diffColorCount;
// if we color 3rd post a different color than the second
diffColorCount = (sameColorCount + diffColorCount) * (k - 1);
// if we color 3rd post the same color as the second
sameColorCount = temp;
}
return sameColorCount + diffColorCount;
}
}