410_分割数组的最大值_binary_search

文章目录

题目描述

在这里插入图片描述

思路

  • 二分
class Solution:
    def splitArray(self, nums: List[int], m: int) -> int:

        def check(x: int) -> bool:
            total, cnt = 0, 1
            for one in nums:
                if total + one > x:
                    cnt += 1
                    total = one
                else:
                    total += one
            return cnt <= m
        
        left = max(nums)
        right = sum(nums)
        while left < right:
            mid = left + (right-left) // 2
            if check(mid):  # True
                right = mid
            else:
                left = mid+1
        return left

猜你喜欢

转载自blog.csdn.net/m0_38024592/article/details/108250993
今日推荐