[Leetcode] Random flip matrix randomly flipped matrices

Source: Internet
Author: User

You is given the number of rows and number of n_rows columns of n_cols a 2D binary matrix where all values is initially 0. Write a function flip which chooses a 0 value uniformly at random, changes it to 1, and then returns the position [row.id, col.id] of that value. Also, write a function reset which sets all values 0. Try to minimize the number of calls to System ' s math.random () and optimize the time and space complexity.

Note:

    1. 1 <= n_rows, n_cols <= 10000
    2. 0 <= row.id < n_rowsand0 <= col.id < n_cols
    3. flipWon't is called when the matrix have no 0 values left.
    4. The total number of calls to and would not flip reset exceed 1000.

Example 1:

["Solution", "Flip", "Flip", "Flip", "Flip")  [null,[0,1],[1,2],[1,0],[1,1]]

Example 2:

["Solution", "Flip", "Flip", "Reset", "Flip")  [null,[0,0],[0,1],null,[0,0]]

Explanation of Input Syntax:

The input is a lists:the subroutines called and their arguments. Solution' s constructor has a arguments, and. and has n_rows n_cols flip reset no arguments. Arguments is always wrapped with a list, even if there aren ' t.

S

[Leetcode] Random flip matrix randomly flipped matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.