leetcode- Week 12 Biweekly game -5111- share chocolate

Subject description:

 

 

 method:

class Solution:
    def maximizeSweetness(self, A: List[int], K: int) -> int:
        def possible(x):
            k, temp = 0, 0
            for a in A:
                temp += a
                if temp >= x:
                    k, temp = k + 1, 0
            return k >= K + 1

        l, h = min(A), sum(A)
            // mid rounded up, in general bipartite = MID +. 1 left, right = MID
            // In the current two points in the left = mid, right = mid - 1 when rounding up it is possible to prevent an infinite loop  
while l < h:
            m = (l + h + 1) // 2
            print(l, h)
            if possible(m):
                l = m
            else:
                h = m - 1
        return l

 

Guess you like

Origin www.cnblogs.com/oldby/p/11713232.html