Split Array Largest Sum

这个题二分的做法已经很经典深入脑中了,基本就是按值二分找最小满足条件的值,这里最诡异的就是检查函数里面的那个迷之起始值1,这个地方为什么是1实在是想不通

class Solution(object):
    def splitArray(self, nums, m):
        """
        :type nums: List[int]
        :type m: int
        :rtype: int
        """
        start, end = max(nums), sum(nums)
        while start + 1 < end
            mid = start + (end - start) / 2
            if self.check(nums, mid, m):
                end = mid
            else:
                start = mid

        if self.check(nums, start, m):
            return start
        return end

    def check(self, nums, count, m):
        tmp = 0
        result = 1
        for num in nums:
            tmp += num
            if tmp > count:
                tmp = num
                result += 1

        return result <= m

results matching ""

    No results matching ""