<p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>

<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>

<ul>
	<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
	<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
</ul>

<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>

<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>

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

<pre>
<strong>Input:</strong> nums = [4,2,5,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong>&nbsp;nums is already continuous.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,5,6]
<strong>Output:</strong> 1
<strong>Explanation:</strong>&nbsp;One possible solution is to change the last element to 4.
The resulting array is [1,2,3,5,4], which is continuous.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,10,100,1000]
<strong>Output:</strong> 3
<strong>Explanation:</strong>&nbsp;One possible solution is to:
- Change the second element to 2.
- Change the third element to 3.
- Change the fourth element to 4.
The resulting array is [1,2,3,4], which is continuous.
</pre>

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

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

'''
Brute Force - Try all combinations of numbers between min and max and see how many operations it takes - O(n2^n)

Some thoughts
- Sorting it seems beneifits because it gives you the min/max already
- Worst case number of operations is to make it from [1 .. len(arr) - 1]
- The other thing is, in general, the result continuous array is one that is increasing by 1 each time until the end

- Matter of finding the starting point
    - If my starting point is X, I need X + 1, X + 2, etc
    - O(n^2) solution
    
Pseudocode
- First get rid of duplicates
- If I know my min, then I know what my max should be, and then how many numbers are in the range of [min, max] in O(1) time
= Binary search to the max, and then just count the indices in between? and the rest has to change

[1 2 5 10 15]
'''

class Solution:
    def minOperations(self, nums: List[int]) -> int:
        nums_without_dups = sorted(list(set(nums)))
        dups = 0
        
        numbers = set()
        for num in nums:
            if num in numbers:
                dups += 1
            else:
                numbers.add(num)
            
        res = len(nums)
        for i, num in enumerate(nums_without_dups):
            cur_min = num
            cur_max = num + len(nums) - 1
            idx = bisect.bisect_left(nums_without_dups, cur_max)
            
            # right side of things
            needed = len(nums_without_dups) - idx - 1 + i
            
            if idx == len(nums_without_dups) or (idx < len(nums_without_dups) and nums_without_dups[idx] != cur_max):
                needed += 1
                
            res = min(res, needed)
  
            
        return res + dups
            
745