{"id":5613,"date":"2019-09-29T20:34:54","date_gmt":"2019-09-30T03:34:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5613"},"modified":"2019-09-29T20:37:37","modified_gmt":"2019-09-30T03:37:37","slug":"leetcode-528-random-pick-with-weight","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-528-random-pick-with-weight\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 528. Random Pick with Weight"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>w<\/code>&nbsp;of positive integers, where&nbsp;<code>w[i]<\/code>&nbsp;describes the weight of index&nbsp;<code>i<\/code>,&nbsp;write a function&nbsp;<code>pickIndex<\/code>&nbsp;which randomly&nbsp;picks an index&nbsp;in proportion&nbsp;to its weight.<\/p>\n\n\n\n<p>Note:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= w.length &lt;= 10000<\/code><\/li><li><code>1 &lt;= w[i] &lt;= 10^5<\/code><\/li><li><code>pickIndex<\/code>&nbsp;will be called at most&nbsp;<code>10000<\/code>&nbsp;times.<\/li><\/ol>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \n<\/strong>[\"Solution\",\"pickIndex\"]\n[[[1]],[]]\n<strong>Output: <\/strong>[null,0]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: \n<\/strong>[\"Solution\",\"pickIndex\",\"pickIndex\",\"pickIndex\",\"pickIndex\",\"pickIndex\"]\n[[[1,3]],[],[],[],[],[]]\n<strong>Output: <\/strong>[null,0,1,1,1,0]<\/pre>\n\n\n\n<p><strong>Explanation of Input Syntax:<\/strong><\/p>\n\n\n\n<p>The input is two lists:&nbsp;the subroutines called&nbsp;and their&nbsp;arguments.&nbsp;<code>Solution<\/code>&#8216;s&nbsp;constructor has one argument, the&nbsp;array&nbsp;<code>w<\/code>.&nbsp;<code>pickIndex<\/code>&nbsp;has no arguments.&nbsp;Arguments&nbsp;are&nbsp;always wrapped with a list, even if there aren&#8217;t any.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Crate a cumulative weight array, random sample a &#8220;weight&#8221;, do a binary search to see which bucket that weight falls in.<br>e.g. w = [2, 3, 1, 4], sum = [2, 5, 6, 10]<br>sample 3 =&gt; index = 1<br>sample 7 =&gt; index = 3<\/p>\n\n\n\n<p>Time complexity: Init: O(n) Pick: O(logn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  Solution(vector<int> w): sums_(std::move(w)) {\n    partial_sum(begin(sums_), end(sums_), begin(sums_));    \n  }\n\n  int pickIndex() {\n    int s = rand() % sums_.back();\n    return upper_bound(begin(sums_), end(sums_), s) - begin(sums_);\n  }\nprivate:  \n  vector<int> sums_;\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;w&nbsp;of positive integers, where&nbsp;w[i]&nbsp;describes the weight of index&nbsp;i,&nbsp;write a function&nbsp;pickIndex&nbsp;which randomly&nbsp;picks an index&nbsp;in proportion&nbsp;to its weight. Note: 1 &lt;= w.length &lt;= 10000 1&#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":[31,177,91],"class_list":["post-5613","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-random","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5613","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=5613"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5613\/revisions"}],"predecessor-version":[{"id":5616,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5613\/revisions\/5616"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}