Skip to content
Problem 519 · LeetCode 519(opens in a new tab)

Random Flip Matrix

MediumHash tables

An m × n matrix starts with every cell set to 0. Build a class that hands out its 0 cells at random, turning each one it hands out into a 1. Each 0 cell must be equally likely to be chosen, and calls to the random number generator should be kept to a minimum.

Implement Solution(m, n), which sets up the all-zero matrix; flip(), which picks a random 0 cell, sets it to 1 and returns its position [row, col]; and reset(), which turns every cell back to 0.

Example 1

Input:  Solution(2, 3), flip, flip, flip, reset, flip
Output: null, [0,1], [1,2], [0,0], null, [1,1]  (one possible run)
Why:    each flip returns a cell no earlier flip returned
2 more examples and the constraints

Example 2

Input:  Solution(1, 2), flip, flip, reset
Output: null, [0,1], [0,0], null  (one possible run)
Why:    after the first flip only one 0 cell is left

Example 3

Input:  Solution(1, 1), flip, reset, flip
Output: null, [0,0], null, [0,0]
Why:    a 1 × 1 matrix has only cell [0,0]

Constraints

  • 1 ≤ m, n ≤ 10,000
  • Whenever flip is called, at least one cell is still 0
  • flip and reset are called at most 1,000 times in total

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Draw until a free cellO(mn / k)
  2. Swap with the last free slotO(1)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRandom calls on a nearly full small matrixVerdict
Draw until a free cellO(mn / k)
Passes; retries when cells run low
Swap with the last free slotO(1)
Best; one random call per flip

m·n = cells in the matrix, k = cells still 0, f = flips since the last reset.

Edge cases to test

  • A 1 × 1 matrix. flip must return [0, 0], and after a reset it can return it again.
  • Flipping every cell. The last flip has one free cell left and must find it; the swap version needs no retries for that.
  • The largest matrix. 10,000 × 10,000 is 10⁸ cells. Do not allocate the matrix or a list of all cells.
  • Reset at any time. A reset before any flip, or several in a row, must leave every cell free.

Common mistakes

  • Building an m × n array or a full list of cells: 10⁸ entries is too much memory.
  • Drawing a row and a column separately and then retrying; it works but wastes random calls, and the retry rate grows as cells fill up.
  • Mixing up the cell number and its position: row is cell // n, column is cell % n, using n (columns), not m.

How to talk through it

  1. 1Clarify. How large can the matrix be? How many calls? Must every free cell be exactly equally likely?
  2. 2Flatten the matrix. Number cells 0 to m·n − 1, so a cell is one integer.
  3. 3Give the simple version. Keep a set of flipped cells and redraw on a hit. Point out it wastes random calls as cells fill up.
  4. 4Shuffle lazily. Draw a slot among the free ones, return its cell, and move the last free cell into the hole. A map stores only the slots that changed.
  5. 5Test out loud. Run a 1 × 1 matrix, flip every cell of a small one, and a reset in the middle.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?