{"id":3339,"date":"2018-07-28T17:08:09","date_gmt":"2018-07-29T00:08:09","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3339"},"modified":"2018-07-28T17:50:50","modified_gmt":"2018-07-29T00:50:50","slug":"leetcode-880-random-pick-with-weight","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-880-random-pick-with-weight\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 880. Random Pick with Weight"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>w<\/code>\u00a0of positive integers, where\u00a0<code>w[i]<\/code>\u00a0describes the weight of index\u00a0<code>i<\/code>,\u00a0write a function\u00a0<code>pickIndex<\/code>\u00a0which randomly\u00a0picks an index\u00a0in proportion\u00a0to its weight.<\/p>\n<p>Note:<\/p>\n<ol>\n<li><code>1 &lt;= w.length &lt;= 10000<\/code><\/li>\n<li><code>1 &lt;= w[i] &lt;= 10^5<\/code><\/li>\n<li><code>pickIndex<\/code>\u00a0will be called at most\u00a0<code>10000<\/code>\u00a0times.<\/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\",\"pickIndex\"]\r\n<\/span><span id=\"example-input-1-2\">[[[1]],[]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[null,0]<\/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\",\"pickIndex\",\"pickIndex\",\"pickIndex\",\"pickIndex\",\"pickIndex\"]\r\n<\/span><span id=\"example-input-2-2\">[[[1,3]],[],[],[],[],[]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[null,0,1,1,1,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\u00a0constructor has one argument, the\u00a0array\u00a0<code>w<\/code>.\u00a0<code>pickIndex<\/code>\u00a0has no arguments.\u00a0Arguments\u00a0are\u00a0always wrapped with a list, even if there aren&#8217;t any.<\/p>\n<h1><strong>Solution: Binary Search<\/strong><\/h1>\n<ol>\n<li>Convert PDF to CDF<\/li>\n<li>Uniformly sample a value s in [1, sum(weights)].<\/li>\n<li>Use binary search to find first index such that PDF[index] &gt;= s.<\/li>\n<\/ol>\n<p>Time complexity: Init O(n), query O(logn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 184 ms\r\nclass Solution {\r\npublic:\r\n  Solution(vector&lt;int&gt; w) { \r\n    sums_ = std::move(w);\r\n    for (int i = 1; i &lt; sums_.size(); ++i)\r\n      sums_[i] += sums_[i - 1];\r\n  }\r\n\r\n  int pickIndex() {\r\n    int s = rand() \/ static_cast&lt;double&gt;(RAND_MAX) * sums_.back() + 1;\r\n    return lower_bound(sums_.begin(), sums_.end(), s) - sums_.begin();\r\n    \/\/ or\r\n    \/\/ int l = 0;\r\n    \/\/ int r = sums_.size();\r\n    \/\/ while (l &lt; r) {\r\n    \/\/   int m = (r - l) \/ 2 + l;\r\n    \/\/   if (sums_[m] &lt; s)\r\n    \/\/     l = m + 1;\r\n    \/\/   else\r\n    \/\/     r = m;\r\n    \/\/ }\r\n    \/\/ return l;\r\n  }\r\nprivate:  \r\n  vector&lt;int&gt; sums_;\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-882-random-point-in-non-overlapping-rectangles\/\">\u82b1\u82b1\u9171 LeetCode 882. Random Point in Non-overlapping Rectangles<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0w\u00a0of positive integers, where\u00a0w[i]\u00a0describes the weight of index\u00a0i,\u00a0write a function\u00a0pickIndex\u00a0which randomly\u00a0picks an index\u00a0in proportion\u00a0to its weight. Note: 1 &lt;= w.length &lt;= 10000&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[52,355,31,177,356,257,91],"class_list":["post-3339","post","type-post","status-publish","format-standard","hentry","category-math","tag-binary-search","tag-cdf","tag-math","tag-medium","tag-pdf","tag-pick","tag-random","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3339","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=3339"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3339\/revisions"}],"predecessor-version":[{"id":3349,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3339\/revisions\/3349"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}