modify code

pull/6/head
左程云 5 years ago
parent 8acbafc3b2
commit b8b42156ef

@ -40,7 +40,7 @@ public class Code01_SubsquenceMaxModM {
for (int j = 1; j <= sum; j++) {
dp[i][j] = dp[i - 1][j];
if (j - arr[i] >= 0) {
dp[i][j] = dp[i][j] | dp[i - 1][j - arr[i]];
dp[i][j] |= dp[i - 1][j - arr[i]];
}
}
}
@ -51,11 +51,11 @@ public class Code01_SubsquenceMaxModM {
}
}
return ans;
}
public static int max3(int[] arr, int m) {
int N = arr.length;
// 0...m-1
boolean[][] dp = new boolean[N][m];
for (int i = 0; i < N; i++) {
dp[i][0] = true;
@ -66,11 +66,10 @@ public class Code01_SubsquenceMaxModM {
// dp[i][j] T or F
dp[i][j] = dp[i - 1][j];
int cur = arr[i] % m;
if (j - cur >= 0) {
dp[i][j] = dp[i][j] | dp[i - 1][j - cur];
}
if (j - cur < 0) {
dp[i][j] = dp[i][j] | dp[i - 1][m + j - cur];
if (cur <= j) {
dp[i][j] |= dp[i - 1][j - cur];
} else {
dp[i][j] |= dp[i - 1][m + j - cur];
}
}
}
@ -132,7 +131,7 @@ public class Code01_SubsquenceMaxModM {
int ans3 = max3(arr, m);
int ans4 = max4(arr, m);
if (ans1 != ans2 || ans2 != ans3 || ans3 != ans4) {
System.out.print("Oops!");
System.out.println("Oops!");
}
}
System.out.println("test finish!");

@ -56,6 +56,50 @@ public class Code02_SnacksWaysMain {
return ways + 1;
}
// arr 30
// func(arr, 0, 14, 0, bag, map)
// func(arr, 15, 29, 0, bag, map)
// 从index出发到end结束
// 之前的选择已经形成的累加和sum
// 零食[index....end]自由选择出来的所有累加和不能超过bag每一种累加和对应的方法数填在map里
// 最后不能什么货都没选
// [3,3,3,3] bag = 6
// 0 1 2 3
// - - - - 0 -> 0 : 1
// - - - $ 3 -> 0 : 1(3, 1)
// - - $ - 3 -> 0 : 1(3, 2)
public static long func(int[] arr, int index, int end, long sum, long bag, TreeMap<Long, Long> map) {
if(sum > bag) {
return 0;
}
// sum <= bag
if(index > end) { // 所有商品自由选择完了!
// sum
if(sum != 0) {
if (!map.containsKey(sum)) {
map.put(sum, 1L);
} else {
map.put(sum, map.get(sum) + 1);
}
return 1;
} else {
return 0;
}
}
// sum <= bag 并且 index <= end(还有货)
// 1) 不要当前index位置的货
long ways = func(arr, index + 1, end, sum, bag, map);
// 2) 要当前index位置的货
ways += func(arr, index + 1, end, sum + arr[index], bag, map);
return ways;
}
public static long process(int[] arr, int index, long w, int end, int bag, TreeMap<Long, Long> map) {
if (w > bag) {
return 0;

@ -0,0 +1,114 @@
package class41;
public class Code02_StoneMerge {
public static int[] sum(int[] arr) {
int N = arr.length;
int[] s = new int[N + 1];
s[0] = 0;
for (int i = 0; i < N; i++) {
s[i + 1] = s[i] + arr[i];
}
return s;
}
public static int w(int[] s, int l, int r) {
return s[r + 1] - s[l];
}
public static int min1(int[] arr) {
if (arr == null || arr.length < 2) {
return 0;
}
int N = arr.length;
int[] s = sum(arr);
return process1(0, N - 1, s);
}
public static int process1(int L, int R, int[] s) {
if (L == R) {
return 0;
}
int next = Integer.MAX_VALUE;
for (int leftEnd = L; leftEnd < R; leftEnd++) {
next = Math.min(next, process1(L, leftEnd, s) + process1(leftEnd + 1, R, s));
}
return next + w(s, L, R);
}
public static int min2(int[] arr) {
if (arr == null || arr.length < 2) {
return 0;
}
int N = arr.length;
int[] s = sum(arr);
int[][] dp = new int[N][N];
for (int L = N - 2; L >= 0; L--) {
for (int R = L + 1; R < N; R++) {
int next = Integer.MAX_VALUE;
for (int leftEnd = L; leftEnd < R; leftEnd++) {
next = Math.min(next, dp[L][leftEnd] + dp[leftEnd + 1][R]);
}
dp[L][R] = next + w(s, L, R);
}
}
return dp[0][N - 1];
}
public static int min3(int[] arr) {
if (arr == null || arr.length < 2) {
return 0;
}
int N = arr.length;
int[] s = sum(arr);
int[][] dp = new int[N][N];
int[][] best = new int[N][N];
for (int i = 0; i < N - 1; i++) {
best[i][i + 1] = i;
dp[i][i + 1] = w(s, i, i + 1);
}
for (int L = N - 3; L >= 0; L--) {
for (int R = L + 2; R < N; R++) {
int next = Integer.MAX_VALUE;
int choose = -1;
for (int leftEnd = best[L][R - 1]; leftEnd <= best[L + 1][R]; leftEnd++) {
int cur = dp[L][leftEnd] + dp[leftEnd + 1][R];
if (cur < next) {
next = cur;
choose = leftEnd;
}
}
best[L][R] = choose;
dp[L][R] = next + w(s, L, R);
}
}
return dp[0][N - 1];
}
public static int[] randomArray(int len, int maxValue) {
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = (int) (Math.random() * maxValue);
}
return arr;
}
public static void main(String[] args) {
int N = 15;
int maxValue = 100;
int testTime = 1000;
System.out.println("test begin");
for (int i = 0; i < testTime; i++) {
int len = (int) (Math.random() * N);
int[] arr = randomArray(len, maxValue);
int ans1 = min1(arr);
int ans2 = min2(arr);
int ans3 = min3(arr);
if (ans1 != ans2 || ans1 != ans3) {
System.out.println("Oops!");
}
}
System.out.println("test end");
}
}

@ -1,6 +1,6 @@
package class41;
public class Code02_SplitArrayLargestSum {
public class Code03_SplitArrayLargestSum {
public static int splitArray1(int[] nums, int m) {
return process(nums, 0, m);
Loading…
Cancel
Save