528. Random Pick with Weight index的随机发生器
[抄题]:
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 <= w.length <= 100001 <= w[i] <= 10^5pickIndexwill be called at most10000times.
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的随机发生器的更多相关文章
- [LeetCode] Random Pick with Weight 根据权重随机取点
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- LeetCode 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...
- 【LeetCode】528. Random Pick with Weight 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...
- 528. Random Pick with Weight
1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...
- [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 ...
- [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 ...
- Random Pick with Weight
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- select random item with weight 根据权重随机选出
http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/ 类似俄罗斯轮盘赌
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
随机推荐
- 使外部主机可访问Django服务
欲让外部主机可访问Django的服务器,需使用如下命令开启服务 python manage.py runserver 0.0.0.0:8000
- linux查看进程启动的时间点
ps -ef |grep xxx # 先查找进程pid ps -wo pid,lstart -p {pid}
- Qt对`vtable的未定义引用
错误描述:Qt在linux系统编译时,遇到一个错误大致如下形式 在 xxxxx函数中 对‘vtable for xxxxx未定义的引用 网上找了很多,各种情况都有,大多数是虚函数未实现导致的, 但也有 ...
- 使用lite-server
进入项目根目录,执行下列步骤 安装lite-server npm install lite-server 新建配置文件bs-config.json bs-config.json中可以: 指定监听的端口 ...
- [UE4]Wrap Box流布局
一.Wrap Box的子控件可以根据Wrap Box的大小自动换行 1.Wrap Box.Inner Slot Padding:Wrap Box所有子控件留白,可以实现每个控件之间的间距都是相同,但是 ...
- Linux第八章:文件,文件系统的压缩,打包备份
压缩:gzip -v 文件名 1:压缩后成 文件名.gz 的压缩文件,原文件消失 2:压缩的文件可以直接使用zcat 文件名.gz 读取里面的内容 解压缩: gunzip 文件名.gz 替 ...
- 杂谈1.py
Python命名规则: 1. 组成:数字/字母/下划线 只能以字母,下划线开头 不能包含空格 避免Python关键字和函数名 简短且具有描述性 描述数据形态及支持操作 Python动态类型 变量无类型 ...
- linux服务samba与ftp篇
samba复习: 1.下载samba:yum -y install samba 2.打开配置文件/etc/samba/smb.conf输入: [共享文件名] path = 目录名 (事先创建) pub ...
- Java double 加、减、乘、除
double类型的数值接相加的时候,结果可能出现精度误差为此Java提供了高精度计算的方法:java.math.*里面提供了BigDecimal类 import org.junit.Test; imp ...
- PHP判断是手机端还是PC访问
function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ' ...