[Swift]LeetCode519. 隨機翻轉矩陣 | Random Flip Matrix

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mdilbsrs-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

Note:github

  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:app

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

Explanation of Input Syntax:dom

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.函數


題中給出一個 n 行 n 列的二維矩陣(n_rows,n_cols),且全部值被初始化爲 0。要求編寫一個 flip 函數,均勻隨機的將矩陣中的 0 變爲 1,並返回該值的位置下標 [row_id,col_id];一樣編寫一個 reset 函數,將全部的值都從新置爲 0。儘可能最少調用隨機函數 Math.random(),而且優化時間和空間複雜度。優化

注意:spa

1.1 <= n_rows, n_cols <= 10000code

2. 0 <= row.id < n_rows 而且 0 <= col.id < n_cols

3.當矩陣中沒有值爲 0 時,不能夠調用 flip 函數

4.調用 flip 和 reset 函數的次數加起來不會超過 1000 次

示例 1:

輸入: 
["Solution","flip","flip","flip","flip"]
[[2,3],[],[],[],[]]
輸出: [null,[0,1],[1,2],[1,0],[1,1]]

示例 2:

輸入: 
["Solution","flip","flip","reset","flip"]
[[1,2],[],[],[],[]]
輸出: [null,[0,0],[0,1],null,[0,0]]

輸入語法解釋:

輸入包含兩個列表:被調用的子程序和他們的參數。Solution 的構造函數有兩個參數,分別爲 n_rows和 n_colsflip 和 reset 沒有參數,參數總會以列表形式給出,哪怕該列表爲空


Runtime: 100 ms
Memory Usage: 19.8 MB
 1 class Solution {
 2     var row:Int
 3     var col:Int
 4     var size:Int    
 5     var m:[Int:Int]
 6 
 7     init(_ n_rows: Int, _ n_cols: Int) {
 8         row = n_rows
 9         col = n_cols
10         size = row * col   
11         m = [Int:Int]()
12     }
13     
14     func flip() -> [Int] {
15         var id:Int = Int.random(in:0..<size)
16         var val:Int = id
17         size -= 1
18         if m[id] != nil {id = m[id]!}
19         m[val] = m[size] != nil ? m[size] : size
20         return [id / col, id % col]      
21     }
22     
23     func reset() {
24         m = [Int:Int]()
25         size = row * col
26     }
27 }
28 
29 /**
30  * Your Solution object will be instantiated and called as such:
31  * let obj = Solution(n_rows, n_cols)
32  * let ret_1: [Int] = obj.flip()
33  * obj.reset()
34  */
35  
相關文章
相關標籤/搜索