-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoney_Problem.java
More file actions
69 lines (61 loc) · 1.95 KB
/
Copy pathMoney_Problem.java
File metadata and controls
69 lines (61 loc) · 1.95 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
package dp;
import org.junit.Test;
/**
* @Author: wei1
* @Date: Create in 2018/11/27 16:40
* @Description: dp
* 给你一个数组arr, 和一个整数aim。 如果可以任意选择arr中的
* 数字, 能不能累加得到aim, 返回true或者false
*/
public class Money_Problem {
public boolean money1(int[] arr, int aim) {
if (arr == null || arr.length == 0) {
return false;
}
return process1(arr, 0, 0, aim);
}
//用子序列来2^n
private boolean process1(int[] arr, int i, int sum, int aim) {
if (sum == aim) {
return true;
}
if (i == arr.length) {
return false;
}
//先写出不是dp的代码,根据这个代码的下一行逻辑生成dp[][]数组★★★★★
return process1(arr, i + 1, sum, aim) || process1(arr, i + 1, sum + arr[i], aim);
}
public boolean money2(int[] arr, int aim) {
if (arr == null || arr.length == 0) {
return false;
}
return process2(arr, aim);
}
private boolean process2(int[] arr, int aim) {
int row = arr.length + 1;
int col = aim + 1;
//java中数组默认初始化为false
boolean[][] dp = new boolean[row][col];
for (int j = 0; j < row; j++) {
dp[j][col-1] = true;
}
// process1(arr, i + 1, sum, aim) || process1(arr, i + 1, sum + arr[i], aim);
for (int i = row - 2; i >= 0; i--) {
for (int j = col - 2; j >= 0; j--) {
dp[i][j] = dp[i + 1][j];
//理解这里的j就是sum
if (j + arr[i] <= aim) {
dp[i][j] = dp[i][j] || dp[i + 1][j + arr[i]];
}
}
}
return dp[0][0];
}
@Test
public void test() {
int[] arr = {1, 4, 9};
int aim = 9;
System.out.println(money1(arr, aim));
System.out.println(money2(arr, aim));
}
}