Leetcode 410 Solution

This article provides solution to leetcode question 410 (split-array-largest-sum)

https://leetcode.com/problems/split-array-largest-sum

Solution

class Solution:
    def canSplit(self, nums, m, s):
        left = 0
        needed = 0

        for num in nums:
            if num > left:
                needed += 1
                left = s
            left -= num

        return needed <= m

    def splitArray(self, nums: List[int], m: int) -> int:
        l = max(nums)
        r = sum(nums)
        while l < r:
            mid = (l + r) // 2
            if self.canSplit(nums, m, mid):
                r = mid
            else:
                l = mid + 1
        return l