<p>There is an <code>m x n</code> binary grid <code>matrix</code> with all the values set <code>0</code> initially. Design an algorithm to randomly pick an index <code>(i, j)</code> where <code>matrix[i][j] == 0</code> and flips it to <code>1</code>. All the indices <code>(i, j)</code> where <code>matrix[i][j] == 0</code> should be equally likely to be returned.</p>

<p>Optimize your algorithm to minimize the number of calls made to the <strong>built-in</strong> random function of your language and optimize the time and space complexity.</p>

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

<ul>
	<li><code>Solution(int m, int n)</code> Initializes the object with the size of the binary matrix <code>m</code> and <code>n</code>.</li>
	<li><code>int[] flip()</code> Returns a random index <code>[i, j]</code> of the matrix where <code>matrix[i][j] == 0</code> and flips it to <code>1</code>.</li>
	<li><code>void reset()</code> Resets all the values of the matrix to be <code>0</code>.</li>
</ul>

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

<pre>
<strong>Input</strong>
[&quot;Solution&quot;, &quot;flip&quot;, &quot;flip&quot;, &quot;flip&quot;, &quot;reset&quot;, &quot;flip&quot;]
[[3, 1], [], [], [], [], []]
<strong>Output</strong>
[null, [1, 0], [2, 0], [0, 0], null, [2, 0]]

<strong>Explanation</strong>
Solution solution = new Solution(3, 1);
solution.flip();  // return [1, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
solution.flip();  // return [2, 0], Since [1,0] was returned, [2,0] and [0,0]
solution.flip();  // return [0, 0], Based on the previously returned indices, only [0,0] can be returned.
solution.reset(); // All the values are reset to 0 and can be returned.
solution.flip();  // return [2, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
</pre>

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

<ul>
	<li><code>1 &lt;= m, n &lt;= 10<sup>4</sup></code></li>
	<li>There will be at least one free cell for each call to <code>flip</code>.</li>
	<li>At most <code>1000</code> calls will be made to <code>flip</code> and <code>reset</code>.</li>
</ul>

class Solution:
    # this problem reduces to sampling k elements without replacement
    # from a known amount N
    
    # why?
    # view the matrix m x n as a 1d mn array (N = mn)
    # everytime we flip an index to 1 (i.e. sample x uniformly from remaining
    # and convert it to its proper 2d form), we cannot flip it again
    # this is the without replacement

    # notice in the reset we just reset our total; why?
    # we don't care what the permutation in our "virtual array" is at the begnining
    def __init__(self, m: int, n: int):
        self.m = m
        self.n = n
        self.total = m * n
        self.d = {}
        
    # d stores the possible swapping of indices for sampling
    # what does d[x] = y mean?
    # d is the "true ordering under permutation"
    # we can only index our virtual array with d
    # our virtual array initially starts as just 0, 1, ..., N-1
    # another way to interpret this is d[x] = x
    # this is why we say the true position will be to remove is not
    # i = random.randint(...) since it is an index, instead
    # position to remove is x = d[i]
    # 

    def flip(self) -> List[int]:
        # i indexes (randomly) the virtual array of size self.total
        # this is the one to be removed (under d)
        i = random.randint(0, self.total-1)
        self.total -= 1
        
        # get x, the true number to remove
        x = self.d.get(i, i)

        # before we remove x, make sure a swap on d[total] is done
        # (i.e. d[total] = y, need to do d[i] = y since 0 <= i < total
        # and the array is currently 0, ..., total-1 indexes)
        self.d[i] = self.d.get(self.total, self.total)
        
        # place the random number to return right outside the virtual array
        self.d[self.total] = x
        return [x//self.n, x%self.n]
        

    def reset(self) -> None:
        self.total = self.m * self.n
        


# Your Solution object will be instantiated and called as such:
# obj = Solution(m, n)
# param_1 = obj.flip()
# obj.reset()
17200