Skip to content

Commit b8b4215

Browse files
committed
modify code
1 parent 8acbafc commit b8b4215

File tree

4 files changed

+166
-9
lines changed

4 files changed

+166
-9
lines changed

src/class39/Code01_SubsquenceMaxModM.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static int max2(int[] arr, int m) {
4040
for (int j = 1; j <= sum; j++) {
4141
dp[i][j] = dp[i - 1][j];
4242
if (j - arr[i] >= 0) {
43-
dp[i][j] = dp[i][j] | dp[i - 1][j - arr[i]];
43+
dp[i][j] |= dp[i - 1][j - arr[i]];
4444
}
4545
}
4646
}
@@ -51,11 +51,11 @@ public static int max2(int[] arr, int m) {
5151
}
5252
}
5353
return ans;
54-
5554
}
5655

5756
public static int max3(int[] arr, int m) {
5857
int N = arr.length;
58+
// 0...m-1
5959
boolean[][] dp = new boolean[N][m];
6060
for (int i = 0; i < N; i++) {
6161
dp[i][0] = true;
@@ -66,11 +66,10 @@ public static int max3(int[] arr, int m) {
6666
// dp[i][j] T or F
6767
dp[i][j] = dp[i - 1][j];
6868
int cur = arr[i] % m;
69-
if (j - cur >= 0) {
70-
dp[i][j] = dp[i][j] | dp[i - 1][j - cur];
71-
}
72-
if (j - cur < 0) {
73-
dp[i][j] = dp[i][j] | dp[i - 1][m + j - cur];
69+
if (cur <= j) {
70+
dp[i][j] |= dp[i - 1][j - cur];
71+
} else {
72+
dp[i][j] |= dp[i - 1][m + j - cur];
7473
}
7574
}
7675
}
@@ -132,7 +131,7 @@ public static void main(String[] args) {
132131
int ans3 = max3(arr, m);
133132
int ans4 = max4(arr, m);
134133
if (ans1 != ans2 || ans2 != ans3 || ans3 != ans4) {
135-
System.out.print("Oops!");
134+
System.out.println("Oops!");
136135
}
137136
}
138137
System.out.println("test finish!");

src/class39/Code02_SnacksWaysMain.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,50 @@ public static long ways(int[] arr, int bag) {
5656
return ways + 1;
5757
}
5858

59+
60+
61+
62+
// arr 30
63+
// func(arr, 0, 14, 0, bag, map)
64+
65+
// func(arr, 15, 29, 0, bag, map)
66+
67+
// 从index出发,到end结束
68+
// 之前的选择,已经形成的累加和sum
69+
// 零食[index....end]自由选择,出来的所有累加和,不能超过bag,每一种累加和对应的方法数,填在map里
70+
// 最后不能什么货都没选
71+
// [3,3,3,3] bag = 6
72+
// 0 1 2 3
73+
// - - - - 0 -> (0 : 1)
74+
// - - - $ 3 -> (0 : 1)(3, 1)
75+
// - - $ - 3 -> (0 : 1)(3, 2)
76+
public static long func(int[] arr, int index, int end, long sum, long bag, TreeMap<Long, Long> map) {
77+
if(sum > bag) {
78+
return 0;
79+
}
80+
// sum <= bag
81+
if(index > end) { // 所有商品自由选择完了!
82+
// sum
83+
if(sum != 0) {
84+
if (!map.containsKey(sum)) {
85+
map.put(sum, 1L);
86+
} else {
87+
map.put(sum, map.get(sum) + 1);
88+
}
89+
return 1;
90+
} else {
91+
return 0;
92+
}
93+
}
94+
// sum <= bag 并且 index <= end(还有货)
95+
// 1) 不要当前index位置的货
96+
long ways = func(arr, index + 1, end, sum, bag, map);
97+
98+
// 2) 要当前index位置的货
99+
ways += func(arr, index + 1, end, sum + arr[index], bag, map);
100+
return ways;
101+
}
102+
59103
public static long process(int[] arr, int index, long w, int end, int bag, TreeMap<Long, Long> map) {
60104
if (w > bag) {
61105
return 0;

src/class41/Code02_StoneMerge.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package class41;
2+
3+
public class Code02_StoneMerge {
4+
5+
public static int[] sum(int[] arr) {
6+
int N = arr.length;
7+
int[] s = new int[N + 1];
8+
s[0] = 0;
9+
for (int i = 0; i < N; i++) {
10+
s[i + 1] = s[i] + arr[i];
11+
}
12+
return s;
13+
}
14+
15+
public static int w(int[] s, int l, int r) {
16+
return s[r + 1] - s[l];
17+
}
18+
19+
public static int min1(int[] arr) {
20+
if (arr == null || arr.length < 2) {
21+
return 0;
22+
}
23+
int N = arr.length;
24+
int[] s = sum(arr);
25+
return process1(0, N - 1, s);
26+
}
27+
28+
public static int process1(int L, int R, int[] s) {
29+
if (L == R) {
30+
return 0;
31+
}
32+
int next = Integer.MAX_VALUE;
33+
for (int leftEnd = L; leftEnd < R; leftEnd++) {
34+
next = Math.min(next, process1(L, leftEnd, s) + process1(leftEnd + 1, R, s));
35+
}
36+
return next + w(s, L, R);
37+
}
38+
39+
public static int min2(int[] arr) {
40+
if (arr == null || arr.length < 2) {
41+
return 0;
42+
}
43+
int N = arr.length;
44+
int[] s = sum(arr);
45+
int[][] dp = new int[N][N];
46+
for (int L = N - 2; L >= 0; L--) {
47+
for (int R = L + 1; R < N; R++) {
48+
int next = Integer.MAX_VALUE;
49+
for (int leftEnd = L; leftEnd < R; leftEnd++) {
50+
next = Math.min(next, dp[L][leftEnd] + dp[leftEnd + 1][R]);
51+
}
52+
dp[L][R] = next + w(s, L, R);
53+
}
54+
}
55+
return dp[0][N - 1];
56+
}
57+
58+
public static int min3(int[] arr) {
59+
if (arr == null || arr.length < 2) {
60+
return 0;
61+
}
62+
int N = arr.length;
63+
int[] s = sum(arr);
64+
int[][] dp = new int[N][N];
65+
int[][] best = new int[N][N];
66+
for (int i = 0; i < N - 1; i++) {
67+
best[i][i + 1] = i;
68+
dp[i][i + 1] = w(s, i, i + 1);
69+
}
70+
for (int L = N - 3; L >= 0; L--) {
71+
for (int R = L + 2; R < N; R++) {
72+
int next = Integer.MAX_VALUE;
73+
int choose = -1;
74+
for (int leftEnd = best[L][R - 1]; leftEnd <= best[L + 1][R]; leftEnd++) {
75+
int cur = dp[L][leftEnd] + dp[leftEnd + 1][R];
76+
if (cur < next) {
77+
next = cur;
78+
choose = leftEnd;
79+
}
80+
}
81+
best[L][R] = choose;
82+
dp[L][R] = next + w(s, L, R);
83+
}
84+
}
85+
return dp[0][N - 1];
86+
}
87+
88+
public static int[] randomArray(int len, int maxValue) {
89+
int[] arr = new int[len];
90+
for (int i = 0; i < len; i++) {
91+
arr[i] = (int) (Math.random() * maxValue);
92+
}
93+
return arr;
94+
}
95+
96+
public static void main(String[] args) {
97+
int N = 15;
98+
int maxValue = 100;
99+
int testTime = 1000;
100+
System.out.println("test begin");
101+
for (int i = 0; i < testTime; i++) {
102+
int len = (int) (Math.random() * N);
103+
int[] arr = randomArray(len, maxValue);
104+
int ans1 = min1(arr);
105+
int ans2 = min2(arr);
106+
int ans3 = min3(arr);
107+
if (ans1 != ans2 || ans1 != ans3) {
108+
System.out.println("Oops!");
109+
}
110+
}
111+
System.out.println("test end");
112+
}
113+
114+
}

src/class41/Code02_SplitArrayLargestSum.java renamed to src/class41/Code03_SplitArrayLargestSum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package class41;
22

3-
public class Code02_SplitArrayLargestSum {
3+
public class Code03_SplitArrayLargestSum {
44

55
public static int splitArray1(int[] nums, int m) {
66
return process(nums, 0, m);

0 commit comments

Comments
 (0)