[抄题]:

Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight.

Note:

  1. 1 <= w.length <= 10000
  2. 1 <= w[i] <= 10^5
  3. pickIndex will be called at most 10000 times.

Example 1:

Input:
["Solution","pickIndex"]
[[[1]],[]]
Output: [null,0]

Example 2:

Input:
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
[[[1,3]],[],[],[],[],[]]
Output: [null,0,1,1,1,0]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么找index

[英文数据结构或算法,为什么不用别的数据结构或算法]:

二分查找index,最后还要在区间内讨论 区间的讨论需要带等号,从而返回一个整数

[一句话思路]:

把index和数字对应起来:在weightsum数组里搜索随机生成的weightsum,然后返回位置。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

找index的二分要考虑相等的情况, 此时end = mid

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

把index和数字对应起来:在weightsum数组里搜索随机生成的weightsum,然后返回位置。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
Random random;
int[] w; public Solution(int[] w) {
//change the w to weightedsum
this.random = random;
for (int i = 1; i < w.length; i++) {
w[i] += w[i - 1];
}
this.w = w;
} public int pickIndex() {
//initilization
int start = 0; int end = w.length - 1;
int randomSum = random.nextInt(w[w.length - 1]) + 1; //search for a range
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (w[mid] == randomSum) {
end = mid;
}
if (w[mid] > randomSum) {
end = mid;
}if (w[mid] < randomSum) {
start = mid;}
} //search for the position
if (randomSum <= w[start]) return start;
else if (randomSum > w[end]) return end + 1;
else if (randomSum <= w[end]) return end; return -1;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(w);
* int param_1 = obj.pickIndex();
*/

528. Random Pick with Weight index的随机发生器的更多相关文章

  1. [LeetCode] Random Pick with Weight 根据权重随机取点

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  2. LeetCode 528. Random Pick with Weight

    原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...

  3. 【LeetCode】528. Random Pick with Weight 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...

  4. 528. Random Pick with Weight

    1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...

  5. [leetcode]528. Random Pick with Weight按权重挑选索引

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  6. [Swift]LeetCode528. 按权重随机选择 | Random Pick with Weight

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  7. Random Pick with Weight

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  8. select random item with weight 根据权重随机选出

    http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/ 类似俄罗斯轮盘赌

  9. [LeetCode] Random Pick with Blacklist 带黑名单的随机选取

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

随机推荐

  1. java Scanner中next和nextLine()区别

    next(): 1.一定要读取到有效字符后才可以结束输入. 2.对输入有效字符之前遇到的空白,next() 方法会自动将其去掉. 3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符. ne ...

  2. 【SpringBoot】整合定时任务和异步任务

    ========================10.SpringBoot整合定时任务和异步任务处理 =============================== 1.SpringBoot定时任务s ...

  3. Mathematics for Computer Science (Eric Lehman / F Thomson Leighton / Albert R Meyer 著)

    I Proofs1 What is a Proof?2 The Well Ordering Principle3 Logical Formulas4 Mathematical Data Types5 ...

  4. java:try...catch...finally

    try...catch...finally 规则: 可以没有 finally 块 如果没有 catch 块,则必须跟一个 finally 块 当在 try 块或 catch 块中遇到 return 语 ...

  5. utf-8mb4和排序规则

    MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode. 最新的 UTF-8 规范只使用一到四个字节,最大能编码21位, ...

  6. 1.2.1 Excel如何从身份证号中提取日期

    在对应的单元格中我们输入公式: =MID(B3,7,4)&"年"&MID(B3,11,2)&"月"&MID(B3,13,2)&a ...

  7. ueditor的简单用法

    先粘贴未使用ueditor之前的代码: <body> <label for="input_content">作答区:</label> <t ...

  8. 用网站把图标做成iconFont文件引用

    1,把psd文件另存为eps文件(ai能打开的格式),前提图标有路径, 2,用ai打开eps文件 3,新建一个空白文件100*100,然后把图标复制进来,等比拉宽至最大化 4,如果图标有蒙版,就点击图 ...

  9. note 3 变量与简单I/O

    变量(Variable) 用于引用(绑定对象的标识符) 语法 变量名=对象(数值.表达式等) 增量赋值运算符 count = count + 1 简写 count += 1 标识符(Identifie ...

  10. Sqlite之事务

    12.Sqlite事务介绍: 11.android SQLite 批量插入数据慢的解决方案 (针对于不同的android api 版本) ========== 12.Sqlite事务介绍: 应用程序初 ...