diff --git a/java/0410-split-array-largest-sum.java b/java/0410-split-array-largest-sum.java new file mode 100644 index 000000000..192423a00 --- /dev/null +++ b/java/0410-split-array-largest-sum.java @@ -0,0 +1,35 @@ +class Solution { + public int splitArray(int[] nums, int k) { + int start = 0; + int end = 0; + + for (int i = 0; i < nums.length; i++) { + start = Math.max(start, nums[i]); + end += nums[i]; + } + + while (start < end) { + int mid = start + (end - start) / 2; + + // calculate how many pieces you can divide this in with this max sum + int sum = 0; + int pieces = 1; + for(int num : nums) { + if (sum + num > mid) { + sum = num; + pieces++; + } else { + sum += num; + } + } + + if (pieces > k) { + start = mid + 1; + } else { + end = mid; + } + + } + return end; // here start == end + } +} \ No newline at end of file
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: