bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 519. Random Flip Matrix

519. Random Flip Matrix

Leetcode Solutions

Virtual Array with HashMap

  1. Initialize a HashMap to represent the virtual array.
  2. Store the total number of cells totalCells and the number of remaining zeros remainingZeros.
  3. For flip: a. Generate a random index randIndex from 0 to remainingZeros - 1. b. If randIndex is not in the HashMap, use it as the matrix index. Otherwise, use the value from the HashMap. c. Swap the value at randIndex with the value at remainingZeros - 1 in the HashMap. d. Decrease remainingZeros by 1. e. Return the matrix indices corresponding to randIndex.
  4. For reset: a. Clear the HashMap. b. Reset remainingZeros to totalCells.
UML Thumbnail

Square-Root Decomposition

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...