From 1fa033d4edcac1491cb0bb2ccc1b7ddd41b136b6 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Mon, 12 Feb 2024 18:43:15 +0530 Subject: [PATCH] created 0410-split-array-largest-sum.java --- java/0410-split-array-largest-sum.java | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 java/0410-split-array-largest-sum.java 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy