{"id":2138,"date":"2018-03-16T04:52:26","date_gmt":"2018-03-16T11:52:26","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2138"},"modified":"2018-03-16T04:55:03","modified_gmt":"2018-03-16T11:55:03","slug":"leetcode-398-random-pick-index","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-398-random-pick-index\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 398. Random Pick Index"},"content":{"rendered":"<h1>Problem:<\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/random-pick-index\/description\/\">https:\/\/leetcode.com\/problems\/random-pick-index\/description\/<\/a><\/p>\n<p>Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.<\/p>\n<p><b>Note:<\/b><br \/>\nThe array size can be very large. Solution that uses too much extra space will not pass the judge.<\/p>\n<h2><b>Example:<\/b><\/h2>\n<pre class=\"lang:java decode:true\">int[] nums = new int[] {1,2,3,3,3};\r\nSolution solution = new Solution(nums);\r\n\r\n\/\/ pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.\r\nsolution.pick(3);\r\n\r\n\/\/ pick(1) should return 0. Since in the array only nums[0] is equal to 1.\r\nsolution.pick(1);<\/pre>\n<h1>Solution:\u00a0Reservoir sampling<\/h1>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Reservoir_sampling\">https:\/\/en.wikipedia.org\/wiki\/Reservoir_sampling<\/a><\/p>\n<p>Time complexity: O(query * n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 92 ms\r\nclass Solution {\r\npublic:\r\n  Solution(vector&lt;int&gt; nums) {\r\n    nums_ = std::move(nums);\r\n  }\r\n\r\n  int pick(int target) {\r\n    int n = 0;    \r\n    int index = -1;\r\n    for (int i = 0; i &lt; nums_.size(); ++i) {\r\n      if (nums_[i] != target) continue;\r\n      ++n;\r\n      if (rand() % n == 0) index = i;\r\n    }\r\n    return index;\r\n  }\r\nprivate:\r\n  vector&lt;int&gt; nums_;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: https:\/\/leetcode.com\/problems\/random-pick-index\/description\/ Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given&#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":[257,255,258],"class_list":["post-2138","post","type-post","status-publish","format-standard","hentry","category-array","tag-pick","tag-randomization","tag-sample","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2138","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=2138"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2138\/revisions"}],"predecessor-version":[{"id":2141,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2138\/revisions\/2141"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}