Skip to content

Latest commit

 

History

History
31 lines (28 loc) · 784 Bytes

2517.md

File metadata and controls

31 lines (28 loc) · 784 Bytes

2517. Maximum Tastiness of Candy Basket

Solution 1 (time O(nlogn+nlogC), space O(1))

class Solution(object):
    def maximumTastiness(self, price, k):
        """
        :type price: List[int]
        :type k: int
        :rtype: int
        """
        def check(x):
            prev = -float("inf")
            cnt = 0
            for p in price:
                if p - prev >= x:
                    prev = p
                    cnt += 1
            return cnt >= k

        price.sort()
        left, right = 0, price[-1] - price[0]
        while left < right:
            mid = (left + right + 1) // 2
            if check(mid):
                left = mid
            else:
                right = mid - 1
        return left