<p>You are given an integer <code>n</code> and an array of <strong>unique</strong> integers <code>blacklist</code>. Design an algorithm to pick a random integer in the range <code>[0, n - 1]</code> that is <strong>not</strong> in <code>blacklist</code>. Any integer that is in the mentioned range and not in <code>blacklist</code> should be <strong>equally likely</strong> to be returned.</p>

<p>Optimize your algorithm such that it minimizes the number of calls to the <strong>built-in</strong> random function of your language.</p>

<p>Implement the <code>Solution</code> class:</p>

<ul>
	<li><code>Solution(int n, int[] blacklist)</code> Initializes the object with the integer <code>n</code> and the blacklisted integers <code>blacklist</code>.</li>
	<li><code>int pick()</code> Returns a random integer in the range <code>[0, n - 1]</code> and not in <code>blacklist</code>.</li>
</ul>

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

<pre>
<strong>Input</strong>
[&quot;Solution&quot;, &quot;pick&quot;, &quot;pick&quot;, &quot;pick&quot;, &quot;pick&quot;, &quot;pick&quot;, &quot;pick&quot;, &quot;pick&quot;]
[[7, [2, 3, 5]], [], [], [], [], [], [], []]
<strong>Output</strong>
[null, 0, 4, 1, 6, 1, 0, 4]

<strong>Explanation</strong>
Solution solution = new Solution(7, [2, 3, 5]);
solution.pick(); // return 0, any integer from [0,1,4,6] should be ok. Note that for every call of pick,
                 // 0, 1, 4, and 6 must be equally likely to be returned (i.e., with probability 1/4).
solution.pick(); // return 4
solution.pick(); // return 1
solution.pick(); // return 6
solution.pick(); // return 1
solution.pick(); // return 0
solution.pick(); // return 4
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= blacklist.length &lt;= min(10<sup>5</sup>, n - 1)</code></li>
	<li><code>0 &lt;= blacklist[i] &lt; n</code></li>
	<li>All the values of <code>blacklist</code> are <strong>unique</strong>.</li>
	<li>At most <code>2 * 10<sup>4</sup></code> calls will be made to <code>pick</code>.</li>
</ul>

class Solution: 
    # time worst O(n log n) where bl has value and we sort, best O(1) where bl has no value,
    # space worst O(log n), best O(1) where bl has no value
    def __init__(self, n: int, bl: List[int]):        
        self.ranges = []
        if bl:
            bl.sort()
            # if the first number in black list is larger than 0, meaning we have a range
            # append that range
            if 0 < bl[0]:
                self.ranges.append((0,bl[0]-1))
            # calculate the diff and genrate the range
            for i in range(1,len(bl)):
                diff = bl[i] - bl[i-1]
                if diff > 1:
                    self.ranges.append((bl[i-1]+1,bl[i]-1))
            # check if the is an end range, add it if so
            if n-1 - bl[-1] > 0:
                self.ranges.append((bl[-1]+1,n-1))
        else:
            # if bl is empty, then all numbers within n can be generated
            self.ranges = [(0,n-1)]
        
    # time O(k) + O(m), where k is the amount of unqiue entries in self.ranges
    # space(1) as we are not storing any extra space in pick. 
    def pick(self) -> int:
        # pick subrange and pick a random value from it 
        rangeIndex = random.randint(0,len(self.ranges)-1)
        
        subRange = self.ranges[rangeIndex]
    
        value = random.randint(subRange[0],subRange[1])
        
        return value

'''
    0-N, where is 20
    
    bl = [    5,6,7,8,9,10,   15,16,17,18,19]
    
    bl = [5,6,7,8,9,10,   15,16,17,18,19]
    
    
    0-4, 11-14, 20
    check from zero to the first integer within bl, 
    to see if there is a difference

    0-4
        
    0-bl[0]-1:
    0-4, 
    11-14

    sP  = 0
    res <- to track of interverals
    while sp is less than the len of bl:
        find the first begin value  for current range array 
        check the difference between i and i -1
            if the difference is 1, if so:
                do nothing
            ellif the diff is more than  1, then we have a range:
                insert a range
                
    i also need to check the begin and the end
    
    
    bl[0]-0, if there is diff insert range array at the beginning
    
    n-bl[-1], if there is diff insert range array at the end
            
    How do i find the begin index
    
    [0, 1,2,3,4,  10] 20 
                  i-1  
        i
          
     5

    0-3
    
    2,1
    
    ------------------------------------------------
    have ranges of all the values that we could have.
    
    ex: 
    0-10 for 0 to N
    bl[4,5,6,7,8,9] 
    our ranges would be [0-3,10-10]
    
    constructor:
    check if the black list actually has value,
        it might not be sorted, sort the blacklist
        
        check the beginning to see if there is a different between
        zero and the first element in blacklist
        if so, enter an range entry with zero as begin, and first entry -1 within blacklist
        as the end index.
        
        iterate the black list from 1 to len(bl)-1:
            if the difference is larger than 1:
                we have a range, insert the range with
                bl[i-1]+1 and begin and bl[i+1] as end
                
        check the end to see if there is a different between
        last element bl[-1]   in blacklist and n
        if so, enter an range entry with bl[-1]+1  as begin, and n-1
        as the end index.
        
    if bl has no value:
        generate an only sub range of O->(N-1)
    
    
    
    pick method:
    rangeindex ->generate a range of 0 to len(ranges)-1
    
    use the rangeIndex to pick the subrange and generate the result value within the subrange, 
    and return it. 
    
    [0, 1,2,3,4,  10] 20 
                5-9  11-19
    [1,3,5,7,9,11,13,15,17,19]
    [0,0],[2,2],[6,6],[8,8],[10,10]
'''
26200