<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)519:隨機翻轉(zhuǎn)矩陣

          共 3001字,需瀏覽 7分鐘

           ·

          2022-02-12 06:21

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?隨機翻轉(zhuǎn)矩陣,我們先來看題面:
          https://leetcode-cn.com/problems/random-flip-matrix/

          There is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned.


          Optimize your algorithm to minimize the number of calls made to the built-in random function of your language and optimize the time and space complexity.


          Implement the Solution class:


          Solution(int m, int n) Initializes the object with the size of the binary matrix m and n.

          int[] flip() Returns a random index [i, j] of the matrix where matrix[i][j] == 0 and flips it to 1.

          void reset() Resets all the values of the matrix to be 0.


          給你一個 m x n 的二元矩陣 matrix ,且所有值被初始化為 0 。請你設(shè)計一個算法,隨機選取一個滿足 matrix[i][j] == 0 的下標 (i, j) ,并將它的值變?yōu)?1 。所有滿足 matrix[i][j] == 0 的下標 (i, j) 被選取的概率應(yīng)當均等。
          盡量最少調(diào)用內(nèi)置的隨機函數(shù),并且優(yōu)化時間和空間復(fù)雜度。
          實現(xiàn) Solution 類:
          • Solution(int m, int n) 使用二元矩陣的大小 m 和 n 初始化該對象

          • int[] flip() 返回一個滿足 matrix[i][j] == 0 的隨機下標 [i, j] ,并將其對應(yīng)格子中的值變?yōu)?1

          • void reset() 將矩陣中所有的值重置為 0


          示例? ? ? ? ? ? ? ? ? ? ? ? ?


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

          解釋
          Solution solution = new?Solution(3, 1);
          solution.flip(); // 返回 [1, 0],此時返回 [0,0]、[1,0] 和 [2,0] 的概率應(yīng)當相同
          solution.flip(); // 返回 [2, 0],因為 [1,0] 已經(jīng)返回過了,此時返回 [2,0] 和 [0,0] 的概率應(yīng)當相同
          solution.flip(); // 返回 [0, 0],根據(jù)前面已經(jīng)返回過的下標,此時只能返回 [0,0]
          solution.reset(); // 所有值都重置為 0 ,并可以再次選擇下標返回
          solution.flip(); // 返回 [2, 0],此時返回 [0,0]、[1,0] 和 [2,0] 的概率應(yīng)當相同


          解題


          1.隨機產(chǎn)生[0, remain - 1]來保證uniform distribution,每次抽完把抽中的數(shù)跟remain交換, 因為remain不可能再被抽中了
          2.但是初始化和reset一維數(shù)組需要O(nr * nc) @ 解決方法: 哈希表 O(1)
          -- 初始化和reset時直接clear()
          -- 只有抽到時才會生成

          class?Solution?{
          public:
          ????Solution(int?n_rows, int?n_cols) {
          ????????nr = n_rows, nc = n_cols, remain = nr * nc;
          ????}

          ????vector<int> flip() {
          ????????uniform_int_distribution<int> uni(0, -- remain);
          ????????int?r = uni(rng);
          ????????int?x = S.count(r) ? S[r] : S[r] = r; // 隨機數(shù)對應(yīng)的值, 沒的話就對應(yīng)本身
          ????????S[r] = S.count(remain) ? S[remain] : S[remain] = remain; // remain是下次需要刪除的所以可以把這個值存到S[r]上,因為r已經(jīng)被使用了
          ????????return?{x / nc, x % nc};
          ????}

          ????void?reset()?{
          ????????S.clear();
          ????????remain = nr * nc;
          ????}
          private:
          ????unordered_map<int, int> S;
          ????int?nr, nc, remain;
          ????mt19937 rng{random_device{}()};
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-500題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)501:二叉搜索樹中的眾數(shù)
          LeetCode刷題實戰(zhàn)502:IPO
          LeetCode刷題實戰(zhàn)503:下一個更大元素 II
          LeetCode刷題實戰(zhàn)504:七進制數(shù)
          LeetCode刷題實戰(zhàn)505:迷宮II
          LeetCode刷題實戰(zhàn)506:相對名次
          LeetCode刷題實戰(zhàn)507:完美數(shù)
          LeetCode刷題實戰(zhàn)508:出現(xiàn)次數(shù)最多的子樹元素和
          LeetCode刷題實戰(zhàn)509:斐波那契數(shù)
          LeetCode刷題實戰(zhàn)510:二叉搜索樹中的中序后繼 II
          LeetCode刷題實戰(zhàn)511:游戲玩法分析 I
          LeetCode刷題實戰(zhàn)512:游戲玩法分析 II
          LeetCode刷題實戰(zhàn)513:找樹左下角的值
          LeetCode刷題實戰(zhàn)514:自由之路
          LeetCode刷題實戰(zhàn)515:在每個樹行中找最大值
          LeetCode刷題實戰(zhàn)516:最長回文子序列
          LeetCode刷題實戰(zhàn)517:超級洗衣機

          瀏覽 28
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大香蕉伊人电影网 | 成人毛片网站 | 一本大道久久无码精品一区二区三区 | www肏| 日韩人妻AV |