LeetCode solutions

2600. Maximum Tastiness of Candy Basket

My accepted Python solution to LeetCode problem 2600, Maximum Tastiness of Candy Basket, running in 506ms.

  • Difficulty: Medium
  • Python
  • Runtime 506ms
  • Memory 27.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 506ms, memory 27.9MB, accepted 2026-01-01.

python
class Solution:
    def maximumTastiness(self, price: List[int], k: int) -> int:
        price.sort()
        
        def canSelect(minDiff):
            count = 1
            lastPrice = price[0]
            for p in price[1:]:
                if p - lastPrice >= minDiff:
                    count += 1
                    lastPrice = p
                    if count >= k:
                        return True
            return count >= k
        
        left, right = 0, price[-1] - price[0]
        result = 0
        
        while left <= right:
            mid = (left + right) // 2
            if canSelect(mid):
                result = mid
                left = mid + 1
            else:
                right = mid - 1
        
        return result

Source