diff --git a/src/class23/Code01_SplitSumClosed.java b/src/class23/Code01_SplitSumClosed.java index 605c930..7a95f0e 100644 --- a/src/class23/Code01_SplitSumClosed.java +++ b/src/class23/Code01_SplitSumClosed.java @@ -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 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 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); } }