Practice Random Flip Matrix
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Solution(m,n) initializes an all-zero matrix. flip() uniformly selects a currently zero cell, changes it to one, and returns [row,column]. reset() makes all cells zero again. Minimize random calls and avoid storing the entire potentially large matrix. Practice approach: Flatten indices and simulate sampling without replacement using a sparse swap map. Choose one remaining index, swap its mapped value with the last remaining slot, and shrink the available range. Reset clears the map and restores the count. This provides O(1) expected work per flip with storage proportional to flips.
Source: Random Flip Matrix on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["Solution","flip","flip","reset","flip"] [[1,2],[],[],[],[]]
[null,[0,1],[0,0],null,[0,1]]
The first two flips must select different cells. After reset, either cell may be selected again; this is one possible sequence.
Constraints
Related LeetCode Problems
Practice more problems similar to Random Flip Matrix.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.