# Solving Leetcode Interviews in Seconds with AI: Random Flip Matrix


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "519" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> There is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned. Optimize your algorithm to minimize the number of calls made to the built-in random function of your language and optimize the time and space complexity. Implement the Solution class:  Solution(int m, int n) Initializes the object with the size of the binary matrix m and n. int[] flip() Returns a random index [i, j] of the matrix where matrix[i][j] == 0 and flips it to 1. void reset() Resets all the values of the matrix to be 0.    Example 1:  Input ["Solution", "flip", "flip", "flip", "reset", "flip"] [[3, 1], [], [], [], [], []] Output [null, [1, 0], [2, 0], [0, 0], null, [2, 0]]  Explanation 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.    Constraints:  1 <= m, n <= 104 There will be at least one free cell for each call to flip. At most 1000 calls will be made to flip and reset.  

	# Explanation
	Here's the solution to the problem, focusing on efficiency and optimization:

*   **Key Idea:** Instead of using a full matrix, we maintain a mapping (dictionary) that represents the available indices and their corresponding 'true' values. This avoids the space overhead of storing a large matrix. We treat the available indices as a contiguous range [0, total_cells), and when a cell is flipped, we swap it with the last available cell in the range and shrink the range. This ensures uniform random sampling.
*   **Optimization:** We use a dictionary to store the mapping between the random index generated and the actual index. This approach avoids repeated calls to random number generator and reduces the time complexity.
*   **Complexity:**
    *   Runtime: O(1) for flip and reset operations.
    *   Storage: O(K), where K is the number of flip operations performed (at most 1000).

	
	# Code
	```python
	import random

class Solution:
    def __init__(self, m: int, n: int):
        self.m = m
        self.n = n
        self.total_cells = m * n
        self.mapping = {}  # Maps index to its actual value after flips

    def flip(self) -> list[int]:
        # Generate a random index within the current range
        rand_index = random.randrange(self.total_cells)

        # Get the actual value at the random index. If it has been remapped,
        # use the remapped value; otherwise, it's just the index itself.
        actual_index = self.mapping.get(rand_index, rand_index)

        # Get the actual value of the last element in the range
        last_index = self.total_cells - 1
        last_actual_index = self.mapping.get(last_index, last_index)

        # Swap the random index with the last index. This means the random
        # index now points to what the last index used to point to.
        self.mapping[rand_index] = last_actual_index

        # Decrement the total number of cells, effectively removing the last
        # element from the range.
        self.total_cells -= 1

        # Convert the actual index to row and column
        row = actual_index // self.n
        col = actual_index % self.n

        return [row, col]

    def reset(self) -> None:
        self.total_cells = self.m * self.n
        self.mapping = {}  # Clear the mapping
	```
			
