modify code

pull/6/head
左程云 4 years ago
parent b2f0d7f9ec
commit 02272879ae

@ -1,7 +1,5 @@
package class23;
import java.util.TreeSet;
public class Code01_SplitSumClosed {
public static int right(int[] arr) {
@ -12,19 +10,19 @@ public class Code01_SplitSumClosed {
for (int num : arr) {
sum += num;
}
TreeSet<Integer> ans = new TreeSet<>();
process(arr, 0, 0, 0, ans, sum >> 1);
return ans.last();
return process(arr, 0, sum >> 1);
}
public static void process(int[] arr, int i, int sum, int picks, TreeSet<Integer> ans, int limit) {
public static int process(int[] arr, int i, int rest) {
if (i == arr.length) {
if (sum <= limit) {
ans.add(sum);
}
return 0;
} else {
process(arr, i + 1, sum, picks, ans, limit);
process(arr, i + 1, sum + arr[i], picks + 1, ans, limit);
int p1 = process(arr, i + 1, rest);
int p2 = 0;
if (arr[i] <= rest) {
p2 = arr[i] + process(arr, i + 1, rest - arr[i]);
}
return Math.max(p1, p2);
}
}

Loading…
Cancel
Save