-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumWays.java
More file actions
70 lines (65 loc) · 1.7 KB
/
Copy pathNumWays.java
File metadata and controls
70 lines (65 loc) · 1.7 KB
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
package dp;
import org.junit.Test;
public class NumWays {
/**
* @param n: non-negative integer, n posts
* @param k: non-negative integer, k colors
* @return: an integer, the total number of ways
* <p>
* 514.栅栏染色
* 我们有一个栅栏,它有n个柱子,现在要给柱子染色,有k种颜色可以染。
* 必须保证不存在超过2个相邻的柱子颜色相同,求有多少种染色方案。
* <p>
* 样例
* n = 3, k = 2, return 6
* <p>
* post 1, post 2, post 3
* way1 0 0 1
* way2 0 1 0
* way3 0 1 1
* way4 1 0 0
* way5 1 0 1
*/
//递归大数字n会超出memory
public int numWays1(int n, int k) {
// write your code here
if (n == 0) {
return 0;
} else if (n == 1) {
return k;
} else if (n == 2) {
return k * k;
} else {
return (numWays1(n - 2, k) + numWays1(n - 1, k)) * (k - 1);
}
/**
* 0 0
* 1 2
* 2 4
* 3 6
*/
}
public int numWays2(int n, int k) {
// write your code here
if (n == 0) {
return 0;
} else if (n == 1) {
return k;
} else if (n == 2) {
return k * k;
}
int n1 = k;
int n2 = k * k;
int temp = 0;
for (int i = 3; i <= n; i++) {
temp = (n1 + n2) * (k - 1);
n1 = n2;
n2 = temp;
}
return temp;
}
@Test
public void testNumWays() {
System.out.println(numWays1(3, 2));
}
}