410. Split Array Largest Sum

410. Split Array Largest Sum

原題: 1011. Capacity To Ship Packages Within D Days

class Solution:
    def splitArray(self, nums: List[int], k: int) -> int:
        def weighting(capacity):
            total = 1
            acc = 0
            for weight in nums:
                if acc + weight > capacity:
                    acc = weight
                    total += 1
                else:
                    acc += weight
            return total
        
        left = max(nums)
        right = sum(nums) + 1

        while left < right:
            mid = left + (right - left) // 2
            if weighting(mid) <= k:
                right = mid 
            else:
                left = mid + 1
        
        return left