<p>You are given an array of positive integers <code>price</code> where <code>price[i]</code> denotes the price of the <code>i<sup>th</sup></code> candy and a positive integer <code>k</code>.</p>

<p>The store sells baskets of <code>k</code> <strong>distinct</strong> candies. The <strong>tastiness</strong> of a candy basket is the smallest absolute difference of the <strong>prices</strong> of any two candies in the basket.</p>

<p>Return <em>the <strong>maximum</strong> tastiness of a candy basket.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> price = [13,5,1,8,21,2], k = 3
<strong>Output:</strong> 8
<strong>Explanation:</strong> Choose the candies with the prices [13,5,21].
The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8.
It can be proven that 8 is the maximum tastiness that can be achieved.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> price = [1,3,1], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> Choose the candies with the prices [1,3].
The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2.
It can be proven that 2 is the maximum tastiness that can be achieved.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> price = [7,7,7,7], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> Choosing any two distinct candies from the candies we have will result in a tastiness of 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= k &lt;= price.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= price[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def maximumTastiness(self, price: List[int], k: int) -> int:
        price=sorted(price)
        #print(price)
        i=0
        j=(price[-1]-price[0])//(k-1) #这里要注意二分查找的范围，比如k=3,那么除以2，就是差距最大是最大值减最小值的一半,
        #j=price[-1]
        while i<=j: #这里注意要等于，因为可能出现等于j的情况
            mid=(i+j)//2  #这里注意，
            print(mid) 
            last,count,ki=price[0],1,1
            while count<k and ki<len(price):
                if price[ki]-last>=mid:
                    last,count=price[ki],count+1
                ki+=1
                if count==k:
                    break
            if count==k :
                i=mid+1
            else :
                j=mid-1
        return i-1
'''
这道题的思路是用2分查找可以减出的最大值，而不是试图在两个两个相减的数字里面去寻找，
price =[34,116,83,15,150,56,69,42,26]
k =6
j=(price[-1]-price[0])//(k-1) 开始求出最大差距是27
然后逐个来试，先二分查找，找13，当然这个希望尽可能大值有人会mid加上1，这时用双指针两两相减，看大于13的是否有6个，
因为有6个所以将i设置为 14，14和27求均值是20，然后发现不够6个，然后14，19找均值，是16，然后发现小了，最后找到19，就是结果，因为i=mid+1,所以返回i-1

'''
        
29600