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 <= 10000
1 <= w[i] <= 10^5
pickIndex
will be called at most10000
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的随机发生器的更多相关文章
- [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 ...
随机推荐
- extjs_10_自己定义combotree组件
1.项目截图 2.treedata.json { text : "root", expanded : true, expandable : true, children : [{ ...
- Centos7.4安装配置haproxy和Keepalived补充内容
补充比较杂 1.当master服务恢复正常之后,backup机器收到消息,然后让出vip 下面是master机器服务恢复正常后,backup机器的Keepalived日志 收到master的消息通知, ...
- 简单的shell脚本练习(一)
1:求1000一内的偶数和 方法一: #!/bin/bash #用累加实现1000以内的偶数和 sum= ;i<=;i=i+)) do sum=$(($sum+$i)); done echo $ ...
- [转]Servlet的学习之Filter过滤器技术
本篇将讲诉Servlet中一项非常重要的技术,Filter过滤器技术.通过过滤器,可以对来自客户端的请求进行拦截,进行预处理或者对最终响应给客户端的数据进行处理后再输出. 要想使用Filter过滤器, ...
- LOJ 3057 「HNOI2019」校园旅行——BFS+图等价转化
题目:https://loj.ac/problem/3057 想令 b[ i ][ j ] 表示两点是否可行,从可行的点对扩展.但不知道顺序,所以写了卡时间做数次 m2 迭代的算法,就是每次遍历所有不 ...
- SQL特殊comment语法
SQL 注释的特殊用法: /*!版本号 语句*/ 表示大于等于某个版本是,才执行相应的语句. 在版本为5.7.23的MySQL上做测试如下: 测试1 mysql> select 1 /*!507 ...
- MNIST数据可视化
一.数据准备 二.数据说明 可以看出图片数据在偏移量为第16字节开始存,每28X28字节存放一张手写字图片.而label是从偏移量为第8字节开始存,每个字节存放一个label. 三.matlab201 ...
- Matlab中调用VS编译的exe文件并传递变量 的方法
经历::在网上找了很多方法,都没有实现在matlab中调用vs的exe文件并且能够传递变量参数,一些小细节花费了自己很多时间,比喻忽略了一些空格! 网上很多的方法都是纯粹复制别人的方法,自己都没有去 ...
- Java笔试面试题整理第三波
转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- DBsever工具连接mysql数据库
当我们安装网DBeaver的时候,怎么通过这个工具来连接Mysql数据库呢 像这个地方就按平时你的数据库信息输入就可以了 接下来配置JDBC的内容 重点说一下驱动包的版本问题,因为我安装的mysql是 ...