<p>In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has <code>n</code> empty baskets, the <code>i<sup>th</sup></code> basket is at <code>position[i]</code>, Morty has <code>m</code> balls and needs to distribute the balls into the baskets such that the <strong>minimum magnetic force</strong> between any two balls is <strong>maximum</strong>.</p>

<p>Rick stated that magnetic force between two different balls at positions <code>x</code> and <code>y</code> is <code>|x - y|</code>.</p>

<p>Given the integer array <code>position</code> and the integer <code>m</code>. Return <em>the required force</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/11/q3v1.jpg" style="width: 562px; height: 195px;" />
<pre>
<strong>Input:</strong> position = [1,2,3,4,7], m = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
</pre>

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

<pre>
<strong>Input:</strong> position = [5,4,3,2,1,1000000000], m = 2
<strong>Output:</strong> 999999999
<strong>Explanation:</strong> We can use baskets 1 and 1000000000.
</pre>

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

<ul>
	<li><code>n == position.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= position[i] &lt;= 10<sup>9</sup></code></li>
	<li>All integers in <code>position</code> are <strong>distinct</strong>.</li>
	<li><code>2 &lt;= m &lt;= position.length</code></li>
</ul>

'''

    *       *
    2,3,4,5,10  => 8

    10 / 3 = 4   m = 3

    *        *
    2,3,4,5,10  => 2 + 4 = 6 search (binary search basket 6) => 10
                => 10 + 4 = 14 (there arent baskets after 10)

    => try 3 (4 - 1)

    *     *
    2,3,4,5,10 => 2 + 3 = 5 search basket 5
                  5 + 3 = 8 search basket 8 -> 10
    

    10 / 4 = 3 -> m = 4

    10 / 5 = 2 -> m = 5

    *   *    *
    2,3,4,5,10 => 2 + 2 = 4 search basket 4
                  4 + 2 = 6 search basket 6 -> 10
                 10 + 2 = 12 (there arent baskets after 10)

    => try 1

    * * * *
    2,3,4,5,10 => 2 + 1 = 3
                  3 + 1 = 4
                  4 + 1 = 5


    NOTE: while searching for the max min distance can be done in linear time 
          the constraints dont allow it. the max min distance can be found
          by doing a binary search where the min distance can be 1 and max distance
          is the distance between the last basket and the first basket.

'''

class Solution:
    def maxDistance(self, position: List[int], m: int) -> int:
        n = len(position)

        position.sort(key=lambda pos: pos)

        def is_solution(dist_distance):
            balls = m
            basket = position[0]

            for i in range(1, n):
                if (basket + dist_distance) <= position[i]:
                    basket = position[i]
                    balls -= 1

                    if balls == 1:
                        return True

                    if balls > n - i:
                        return False


            return False
        

        def search_distance(start, end):
            if start == end:
                return start if is_solution(start) else 0

            mid = (start + end) // 2

            if is_solution(mid):
                return max(mid, search_distance(mid + 1, end))
            
            return search_distance(start, mid)


        max_distance = position[n - 1] - position[0]
        min_distance = 1

        return search_distance(min_distance, max_distance)
42000