Leetcode: Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge. Example: int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums); // pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3); // pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);
Three types of answer:
Map Solution, O(N) memory, O(N) init, O(1) pick.
Like @dettier's Reservoir Sampling. O(1) init, O(1) memory, but O(N) to pick.
Reservior Sampling
public class Solution {
int[] arr;
Random random; public Solution(int[] nums) {
arr = nums;
random = new Random();
} public int pick(int target) {
int count = 0;
int index = 0;
for (int i=0; i<arr.length; i++) {
if (arr[i] == target) {
count++;
if (random.nextInt(count) == 0) {
index = i;
}
}
}
return index;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
Map solution: MLE for big case
public class Solution { public Solution(int[] nums) {
for (int i=0; i<nums.length; i++) {
int num = nums[i];
if (!indexes.containsKey(num))
indexes.put(num, new ArrayList<Integer>());
indexes.get(num).add(i);
}
} public int pick(int target) {
List<Integer> indexes = this.indexes.get(target);
int i = (int) (Math.random() * indexes.size());
return indexes.get(i);
} private Map<Integer, List<Integer>> indexes = new HashMap<>();
}
Leetcode: Random Pick Index的更多相关文章
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 398. Random Pick Index - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- [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】398. Random Pick Index 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...
- [LeetCode] 398. Random Pick Index ☆☆☆
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- [leetcode] 398. Random Pick Index
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
- Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 398. Random Pick Index
随机返还target值的坐标(如果存在多个target). 不太明白为什么这个题是M难度的. 无非是要么弄TABLE之类的,开始麻烦点,但是pick的时候直接PICK出来就行了. 要么开始简单点,都存 ...
随机推荐
- HTmlTableTOExcel
function TableTOExcel() { //第一种方法 //var url = 'data:application/vnd.ms-excel;base64,' + encodeURICom ...
- javaWeb中struts开发——Logic标签
1.Struts标签的logic标签 Logic标签是逻辑标签,是Struts中比较重要的标签,完成各种逻辑运算操作,可以直接支持全局调转. 2.1<logic:present><l ...
- locality
Computer Systems A Programmer's Perspective Second Edition Well-written computer programs tend to ex ...
- ArcMap 标注、注记、图形文本
标注.注记.图形文本 2016年8月10日10:29 ArcMap中怎样向地图添加文本,其中标注与注记是重点内容,此处对此进行总结. 参考链接: ①地图文本基本词汇: 什么是文本? ArcGIS 提供 ...
- 解决微软的两个恶心问题(VS2008死机、Win2008 WAS无法启动)
1.Visual Studio 2008,在切换到Web设计界面或Html Markup界面时,过一段时间就出现假死,点击任何地方没反应,也关闭不了,只能用任务管理器结束任务. 上网查了下,应该是先装 ...
- mysql binlog恢复
MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...
- XCode中的单元测试:编写测试类和方法(内容意译自苹果官方文档)
当你在工程中通过测试导航栏添加了一个测试target之后, xcode会在测试导航栏中显示该target所属的测试类和方法. 这一章演示了怎么创建测试类,以及如何编写测试方法. 测试targets, ...
- Qt from Linux to Windows target
45down voteaccepted Just use M cross environment (MXE). It takes the pain out of the whole process: ...
- javascript 模式方面的学习
看了好多网上的文章,基本上得到一个结论:一些写类工具函数或框架的写类方式本质上都是 构造函数+原型 1.用构造函数来定义类属性(字段).2.用原型方式来定义类的方法. 具体文章请参阅 JavaScri ...
- Arcgis for JS之Cluster聚类分析的实现(基于区域范围的)
原文:Arcgis for JS之Cluster聚类分析的实现(基于区域范围的) 咱们书接上文,在上文,实现了基于距离的空间聚类的算法实现,在本文,将继续介绍空间聚类之基于区域范围的实现方式,好了,闲 ...