modify code

pull/6/head
左程云 4 years ago
parent 0ddb8b7bac
commit 8acbafc3b2

@ -1,10 +1,10 @@
package class38; package class39;
import java.util.HashSet; import java.util.HashSet;
import java.util.TreeSet; import java.util.TreeSet;
// 给定一个非负数组arr和一个正数m。 返回arr的所有子序列中累加和%m之后的最大值。 // 给定一个非负数组arr和一个正数m。 返回arr的所有子序列中累加和%m之后的最大值。
public class Code05_SubsquenceMaxModM { public class Code01_SubsquenceMaxModM {
public static int max1(int[] arr, int m) { public static int max1(int[] arr, int m) {
HashSet<Integer> set = new HashSet<>(); HashSet<Integer> set = new HashSet<>();

@ -0,0 +1,79 @@
package class39;
public class Code02_SnacksWays {
public static int ways1(int[] arr, int w) {
// arr[0...]
return process(arr, 0, w);
}
// 从左往右的经典模型
// 还剩的容量是restarr[index...]自由选择,
// 返回选择方案
// index 0N
// rest : 0~w
public static int process(int[] arr, int index, int rest) {
if (rest < 0) { // 没有容量了
// -1 无方案的意思
return -1;
}
// rest>=0,
if (index == arr.length) { // 无零食可选
return 1;
}
// rest >=0
// 有零食index
// index号零食要 or 不要
// index, rest
// (index+1, rest)
// (index+1, rest-arr[i])
int next1 = process(arr, index + 1, rest); // 不要
int next2 = process(arr, index + 1, rest - arr[index]); // 要
return next1 + (next2 == -1 ? 0 : next2);
}
public static int ways2(int[] arr, int w) {
int N = arr.length;
int[][] dp = new int[N + 1][w + 1];
for (int j = 0; j <= w; j++) {
dp[N][j] = 1;
}
for (int i = N - 1; i >= 0; i--) {
for (int j = 0; j <= w; j++) {
dp[i][j] = dp[i + 1][j] + ((j - arr[i] >= 0) ? dp[i + 1][j - arr[i]] : 0);
}
}
return dp[0][w];
}
public static int ways3(int[] arr, int w) {
int N = arr.length;
int[][] dp = new int[N][w + 1];
for (int i = 0; i < N; i++) {
dp[i][0] = 1;
}
if (arr[0] <= w) {
dp[0][arr[0]] = 1;
}
for (int i = 1; i < N; i++) {
for (int j = 1; j <= w; j++) {
dp[i][j] = dp[i - 1][j] + ((j - arr[i]) >= 0 ? dp[i - 1][j - arr[i]] : 0);
}
}
int ans = 0;
for (int j = 0; j <= w; j++) {
ans += dp[N - 1][j];
}
return ans;
}
public static void main(String[] args) {
int[] arr = { 4, 3, 2, 9 };
int w = 8;
System.out.println(ways1(arr, w));
System.out.println(ways2(arr, w));
System.out.println(ways3(arr, w));
}
}

@ -0,0 +1,81 @@
//不要拷贝包信息的内容
package class39;
//本文件是Code02_SnacksWays问题的牛客题目解答
//但是用的分治的方法
//这是牛客的测试链接:
//https://www.nowcoder.com/questionTerminal/d94bb2fa461d42bcb4c0f2b94f5d4281
//把如下的全部代码拷贝进编辑器java
//可以直接通过
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Code02_SnacksWaysMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int bag = sc.nextInt();
int[] arr = new int[N];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
long ways = ways(arr, bag);
System.out.println(ways);
sc.close();
}
public static long ways(int[] arr, int bag) {
if (arr == null || arr.length == 0) {
return 0;
}
if (arr.length == 1) {
return arr[0] <= bag ? 2 : 1;
}
int mid = (arr.length - 1) >> 1;
TreeMap<Long, Long> lmap = new TreeMap<>();
long ways = process(arr, 0, 0, mid, bag, lmap);
TreeMap<Long, Long> rmap = new TreeMap<>();
ways += process(arr, mid + 1, 0, arr.length - 1, bag, rmap);
TreeMap<Long, Long> rpre = new TreeMap<>();
long pre = 0;
for (Entry<Long, Long> entry : rmap.entrySet()) {
pre += entry.getValue();
rpre.put(entry.getKey(), pre);
}
for (Entry<Long, Long> entry : lmap.entrySet()) {
long lweight = entry.getKey();
long lways = entry.getValue();
Long floor = rpre.floorKey(bag - lweight);
if (floor != null) {
long rways = rpre.get(floor);
ways += lways * rways;
}
}
return ways + 1;
}
public static long process(int[] arr, int index, long w, int end, int bag, TreeMap<Long, Long> map) {
if (w > bag) {
return 0;
}
if (index > end) {
if (w != 0) {
if (!map.containsKey(w)) {
map.put(w, 1L);
} else {
map.put(w, map.get(w) + 1);
}
return 1;
} else {
return 0;
}
} else {
long ways = process(arr, index + 1, w, end, bag, map);
ways += process(arr, index + 1, w + arr[index], end, bag, map);
return ways;
}
}
}

@ -2,7 +2,7 @@ package class39;
import java.util.LinkedList; import java.util.LinkedList;
public class Code02_10Ways { public class Code03_10Ways {
public static long ways1(int N) { public static long ways1(int N) {
int zero = N; int zero = N;

@ -1,6 +1,6 @@
package class39; package class39;
public class Code01_DifferentBTNum { public class Code04_DifferentBTNum {
// k(0) = 1, k(1) = 1 // k(0) = 1, k(1) = 1
// //

@ -1,6 +1,6 @@
package class39; package class39;
public class Code04_PrintMatrixSpiralOrder { public class Code05_PrintMatrixSpiralOrder {
public static void spiralOrderPrint(int[][] matrix) { public static void spiralOrderPrint(int[][] matrix) {
int tR = 0; int tR = 0;

@ -1,6 +1,6 @@
package class39; package class39;
public class Code03_RotateMatrix { public class Code06_RotateMatrix {
public static void rotate(int[][] matrix) { public static void rotate(int[][] matrix) {
int a = 0; int a = 0;

@ -1,6 +1,6 @@
package class39; package class39;
public class Code05_ZigZagPrintMatrix { public class Code07_ZigZagPrintMatrix {
public static void printMatrixZigZag(int[][] matrix) { public static void printMatrixZigZag(int[][] matrix) {
int tR = 0; int tR = 0;

@ -1,6 +1,6 @@
package class39; package class39;
public class Code06_PrintStar { public class Code08_PrintStar {
public static void printStar(int N) { public static void printStar(int N) {
int leftUp = 0; int leftUp = 0;
Loading…
Cancel
Save