[LeetCode] Random Flip Matrix 随机翻转矩阵

You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all values are 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 back to 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_rows and 0 <= col.id < n_cols
  3. flip will not be called when the matrix has no 0 values left.
  4. the total number of calls to flip and reset will not exceed 1000.

Example 1:

Input:
["Solution","flip","flip","flip","flip"]
[[2,3],[],[],[],[]]
Output: [null,[0,1],[1,2],[1,0],[1,1]]

Example 2:

Input:
["Solution","flip","flip","reset","flip"]
[[1,2],[],[],[],[]]
Output: [null,[0,0],[0,1],null,[0,0]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution‘s constructor has two arguments, n_rows and n_colsflip and resethave no arguments. Arguments are always wrapped with a list, even if there aren‘t any.

s

原文地址:https://www.cnblogs.com/grandyang/p/9781289.html

时间: 2024-08-29 08:44:57

[LeetCode] Random Flip Matrix 随机翻转矩阵的相关文章

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某

【leetcode】519. Random Flip Matrix

题目如下: You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all values are initially 0. Write a function flip which chooses a 0 value uniformly at random, changes it to 1, and then returns the position [row.

LeetCode 73. Set Matrix Zeros(矩阵赋零)

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O

[C++]LeetCode: 110 Spiral Matrix (螺旋输出矩阵元素)

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路:我们来自己画一个螺旋线的行走轨迹

leetCode 59.Spiral Matrix II (螺旋矩阵II) 解题思路和方法

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路:此题和螺旋矩阵题差不多,没什么难的地方,主要就是四个方向的转换. 具体代码如下: pu

[LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

On a 2 dimensional grid with?R?rows and?C?columns, we start at?(r0, c0)?facing east. Here, the north-west corner of the grid is at the?first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw

LeetCode 59 Spiral Matrix II 螺旋矩阵之二

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 此题跟之前那道本质上没什么区别,就相当于个类似逆运算的过程,这道题是要按螺旋的顺序来填数,由于

[LeetCode] Pyramid Transition Matrix 金字塔转变矩阵

We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like `'Z'`. For every block of color `C` we place not in the bottom row, we are placing it on top of a left block of color `A` and right block of color `B`