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.html
Note:數組
1 <= n_rows, n_cols <= 10000
0 <= row.id < n_rows
and 0 <= col.id < n_cols
flip
will not be called when the matrix has no 0 values left.flip
and reset
will not exceed 1000.Example 1:app
Input:
["Solution","flip","flip","flip","flip"]
[[2,3],[],[],[],[]] Output: [null,[0,1],[1,2],[1,0],[1,1]]
Example 2:dom
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_cols
. flip
and reset
have no arguments. Arguments are always wrapped with a list, even if there aren't any.post
這道題讓咱們隨機翻轉矩陣中的一個位置,因爲以前連續作了好幾道隨機選點的題 Implement Rand10() Using Rand7(),Generate Random Point in a Circle,和 Random Point in Non-overlapping Rectangles。覺得這道題也要用拒絕採樣Rejection Sampling來作,其實不是的。這道題給了咱們一個矩形的長和寬,讓咱們每次隨機翻轉其中的一個點,其中的隱含條件是,以前翻轉過的點,下一次不能再翻轉回來,而咱們隨機生成點是有可能有重複的,一旦不少點都被翻轉後,很大機率會重複生成以前的點,因此咱們須要有去重複的操做,而這也是本題的難點所在。博主最早的想法是,既然有可能生成重複的點,那麼咱們使用一個while循環,只要生成了以前的點,咱們就從新再生成一個,這麼一說,感受又有點像拒絕採樣Rejection Sampling的原理了。無論了,無論黑貓白貓,能抓耗子🐭的就是好貓🐱。題目中說了讓咱們儘可能減小空間使用度,那麼咱們就不能生成整個二維數組了,咱們能夠用一個HashSet來記錄翻轉過了點,這樣也方便咱們進行查重操做。因此咱們每次都隨機出一個長和寬,而後看這個點是否已經在HashSet中了,不在的話,就加入HashSet,而後返回便可,參見代碼以下:優化
解法一:spa
class Solution { public: Solution(int n_rows, int n_cols) { row = n_rows; col = n_cols; } vector<int> flip() { while (true) { int x = rand() % row, y = rand() % col; if (!flipped.count(x * col + y)) { flipped.insert(x * col + y); return {x, y}; } } } void reset() { flipped.clear(); } private: int row, col; unordered_set<int> flipped; };
因爲題目中讓咱們儘可能少用rand()函數,因此咱們能夠進行優化同樣,不在同時生成兩個隨機數,而是隻生成一個,而後拆分出長和寬便可,其餘部分和上面均相同,參見代碼以下:code
解法二:orm
class Solution { public: Solution(int n_rows, int n_cols) { row = n_rows; col = n_cols; } vector<int> flip() { while (true) { int val = rand() % (row * col); if (!flipped.count(val)) { flipped.insert(val); return {val / col, val % col}; } } } void reset() { flipped.clear(); } private: int row, col; unordered_set<int> flipped; };
其實咱們還能夠進一步的優化rand()的調用數,咱們可讓每一個flip()函數只調用一次rand()函數,這該怎麼作呢,這裏就有一些trick了。咱們須要使用一個變量size,初始化爲矩形的長乘以寬,而後仍是隻生成一個隨機數id,並使用另外一個變量val來記錄它。接下來咱們給size自減1,咱們知道 rand() % size 獲得的隨機數的範圍是 [0, size-1],那麼假如第一次隨機出了size-1後,此時size自減1以後,下一次沒必要擔憂還會隨機出size-1,由於此時的size比以前減小了1。若是第一次隨機出了0,假設最開始size=4,那麼此時自減1以後,size=3,此時咱們將0映射到3。那麼下次咱們若是再次隨機出了0,此時size自減1以後,size=2,如今0有映射值,因此咱們將id改成其映射值3,而後再將0映射到2,這樣下次就算再搖出了0,咱們還能夠改變id值。你們有沒有發現,咱們的映射值都是沒有沒使用過的數字,這也是爲啥開始先檢測id是否被使用了,若已經被使用了,則換成其映射值,而後再更新以前的id的映射值,找到下一個未被使用的值便可,參見代碼以下:
解法三:
class Solution { public: Solution(int n_rows, int n_cols) { row = n_rows; col = n_cols; size = row * col; } vector<int> flip() { int id = rand() % size, val = id; --size; if (m.count(id)) id = m[id]; m[val] = m.count(size) ? m[size] : size; return {id / col, id % col}; } void reset() { m.clear(); size = row * col; } private: int row, col, size; unordered_map<int, int> m; };
參考資料:
https://leetcode.com/problems/random-flip-matrix/
https://leetcode.com/problems/random-flip-matrix/discuss/177809/c%2B%2B-solution