diff --git a/javascript/0410-split-array-largest-sum.js b/javascript/0410-split-array-largest-sum.js new file mode 100644 index 000000000..32a30f03a --- /dev/null +++ b/javascript/0410-split-array-largest-sum.js @@ -0,0 +1,41 @@ +/** + * https://leetcode.com/problems/split-array-largest-sum/ + * + * Binary Search + * Time O(log(s)*n) (s = difference between the least and max possible value) | Space O(1) + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var splitArray = function(nums, k) { + + let left = Math.max(...nums); + let right = nums.reduce((acc, num) => acc + num, 0); + let result = right; + while(left <= right) { + const mid = (left + right) >> 1; + if(canSplit(mid)) { + result = mid; + right = mid - 1; + } else { + left = mid + 1; + } + } + + function canSplit(largest) { + let splitCount = 0; + let currSum = 0; + + for(let i = 0; i < nums.length; i++) { + currSum += nums[i]; + if(currSum > largest) { + currSum = nums[i]; + splitCount++; + } + } + + return splitCount + 1 <= k; + } + + return result; +}; 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