Split Array Largest Sum - Divide and Conquer / Binary Search

https://algo.monster/problems/split_array_largest_sum

Maybe I’m misunderstanding, but for canSatisfy to work, don’t you need to split nums into exactly m subarrays? Or is any number of subarrays less than or equal to m satisfactory?

I found the canSatisfy function in the implementation a little confusing. Plus, you don’t need to initialize the min to 0 when it should be initialized to the highest element in nums. Below is my solution with some slight changes.

var splitArray = function(nums, m) {
    let largestSum = 0;
    let min = Math.max(...nums)
    let max = nums.reduce((acc, curr) => acc += curr, 0);
    while (min <= max) {
        const target = Math.floor((min + max) / 2);
        if (isValid(nums, m, target)) {
            max = target - 1;
            largestSum = target;
        } else {
            min = target + 1;
        }
    }
    
    return largestSum;
};

function isValid(nums, m, target) {
    let splits = 0
    let sum = 0;
    
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] > target) return false;
        
        sum += nums[i];
        
        if (sum > target) {
            splits++;
            sum = nums[i];
        }
    }
    
    return splits < m;
}