{"id":3398,"date":"2018-07-31T08:33:05","date_gmt":"2018-07-31T15:33:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3398"},"modified":"2018-07-31T08:36:28","modified_gmt":"2018-07-31T15:36:28","slug":"leetcode-881-random-flip-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-881-random-flip-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 881. Random Flip Matrix"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>You are given the number of rows\u00a0<code>n_rows<\/code>\u00a0and number of columns\u00a0<code>n_cols<\/code>\u00a0of a\u00a02D\u00a0binary matrix\u00a0where all values are initially 0.\u00a0Write a function\u00a0<code>flip<\/code>\u00a0which chooses\u00a0a 0 value\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Discrete_uniform_distribution\" target=\"_blank\" rel=\"noopener\">uniformly at random<\/a>,\u00a0changes it to 1,\u00a0and then returns the position\u00a0<code>[row.id, col.id]<\/code>\u00a0of that value. Also, write a function\u00a0<code>reset<\/code>\u00a0which sets all values back to 0.\u00a0<strong>Try to minimize the number of calls to system&#8217;s Math.random()<\/strong>\u00a0and optimize the time and\u00a0space complexity.<\/p>\n<p>Note:<\/p>\n<ol>\n<li><code>1 &lt;= n_rows, n_cols\u00a0&lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= row.id &lt; n_rows<\/code>\u00a0and\u00a0<code>0 &lt;= col.id &lt; n_cols<\/code><\/li>\n<li><code>flip<\/code>\u00a0will not be called when the matrix has no\u00a00 values left.<\/li>\n<li>the total number of calls to\u00a0<code>flip<\/code>\u00a0and\u00a0<code>reset<\/code>\u00a0will not exceed\u00a01000.<\/li>\n<\/ol>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: \r\n<\/strong><span id=\"example-input-1-1\">[\"Solution\",\"flip\",\"flip\",\"flip\",\"flip\"]\r\n<\/span><span id=\"example-input-1-2\">[[2,3],[],[],[],[]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[null,[0,1],[1,2],[1,0],[1,1]]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: \r\n<\/strong><span id=\"example-input-2-1\">[\"Solution\",\"flip\",\"flip\",\"reset\",\"flip\"]\r\n<\/span><span id=\"example-input-2-2\">[[1,2],[],[],[],[]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[null,[0,0],[0,1],null,[0,0]]<\/span><\/pre>\n<\/div>\n<p><strong>Explanation of Input Syntax:<\/strong><\/p>\n<p>The input is two lists:\u00a0the subroutines called\u00a0and their\u00a0arguments.\u00a0<code>Solution<\/code>&#8216;s constructor\u00a0has two arguments,\u00a0<code>n_rows<\/code>\u00a0and\u00a0<code>n_cols<\/code>.\u00a0<code>flip<\/code>\u00a0and\u00a0<code>reset<\/code>\u00a0have\u00a0no\u00a0arguments.\u00a0Arguments\u00a0are\u00a0always wrapped with a list, even if there aren&#8217;t any.<\/p>\n<h1><strong>Solution 1: Hashtable + Resample<\/strong><\/h1>\n<p>Time complexity: O(|flip|) = O(1000) = O(1)<\/p>\n<p>Space complexity: O(|flip|) =\u00a0O(1000) = O(1)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  Solution(int n_rows, int n_cols): \r\n    rows_(n_rows), cols_(n_cols), n_(rows_ * cols_) {}\r\n\r\n  vector&lt;int&gt; flip() {\r\n    int index = rand() % n_;\r\n    while (m_.count(index))\r\n      index = rand() % n_;    \r\n    m_.insert(index);\r\n    return {index \/ cols_, index % cols_};\r\n  }\r\n\r\n  void reset() {\r\n    m_.clear();\r\n    n_ = rows_ * cols_;\r\n  }\r\nprivate:\r\n  const int rows_;\r\n  const int cols_;\r\n  int n_;\r\n  unordered_set&lt;int&gt; m_;\r\n};<\/pre>\n<h1><strong>Solution 2:\u00a0<\/strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Fisher%E2%80%93Yates_shuffle\">Fisher\u2013Yates shuffle<\/a><\/h1>\n<p>Generate a random shuffle of 0 to n &#8211; 1, one number at a time.<\/p>\n<p>Time complexity: flip: O(1)<\/p>\n<p>Space complexity: O(|flip|) = O(1000) = O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  Solution(int n_rows, int n_cols): \r\n    rows_(n_rows), cols_(n_cols), n_(rows_ * cols_) {}\r\n\r\n  vector&lt;int&gt; flip() {\r\n    int s = rand() % (n_--);\r\n    int index = s;\r\n    if (m_.count(s))\r\n      index = m_[s];\r\n    m_[s] = m_.count(n_) ? m_[n_] : n_;\r\n    return {index \/ cols_, index % cols_};\r\n  }\r\n\r\n  void reset() {\r\n    m_.clear();\r\n    n_ = rows_ * cols_;\r\n  }\r\nprivate:\r\n  const int rows_;\r\n  const int cols_;\r\n  int n_;\r\n  unordered_map&lt;int, int&gt; m_;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem You are given the number of rows\u00a0n_rows\u00a0and number of columns\u00a0n_cols\u00a0of a\u00a02D\u00a0binary matrix\u00a0where all values are initially 0.\u00a0Write a function\u00a0flip\u00a0which chooses\u00a0a 0 value\u00a0uniformly at random,\u00a0changes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[177,91,258,256],"class_list":["post-3398","post","type-post","status-publish","format-standard","hentry","category-array","tag-medium","tag-random","tag-sample","tag-shuffle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3398"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3398\/revisions"}],"predecessor-version":[{"id":3403,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3398\/revisions\/3403"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}