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出来就行了. 要么开始简单点,都存 ...
随机推荐
- MVC 扩展方法特点
.NET MVC 3中扩展方法特点: (1)扩展类的名称以Extensions结尾: (2)扩展类的类型是static: (3)扩展方法至少有一个参数,第一个参数必须指定该方法作用于哪个类型,并且该参 ...
- Nginx 禁用IP IP段
最近公司网站被竞争对手用爬虫频繁访问,所以我们这边要禁止这些爬虫访问,我们通过nginx 指令就可以实现了 方法一:直接在LB机器上封IP 1.在 blocksip.conf 文件中加入要屏蔽的ip或 ...
- ubuntu下opencv在Qt中的使用
1. 编译安装OpenCV2.4.9 本博已有文章描述 2. 安装Qt和QtCreator 从qt-project.org 下载Qt安装文件 qt-opensource-linux-x64-5.4. ...
- 利用sqlserver日志恢复数据
如果你已经急的焦头烂额,看到这篇文章的时候,请你换个坐姿,深呼吸几次,静下心来将这篇文章读完,也许你的问题迎刃而解. 我遇 到的情况是这样的,网站被植入木马,盗取了我的web.config文件,web ...
- 如何获取并分析L2CAP包
本文中的分析与软件相关的内容,都是以WinCE中的 Microsoft Bluetooth Core Stack为例进行分析:与协议有关的内容,是基于Bluetooth Core 2.1 + EDR ...
- 为mutable类型的容器(array,set等)添加kvo,有点麻烦,供参考和了解下吧
http://blog.csdn.net/caryaliu/article/details/49284185 需要在被观察的属性所在的类里面实现一些方法,对开发者不友好,一般不建议使用,这里mark一 ...
- python 之 utf-8编码的秘密
python3的默认编码方案是utf-8编码,看了些资料,来做总结. 要说utf-8,就要说说unicode,要说unicode,就要说ASCII,我们还是慢慢来. 1.ASCII ASCII编码最初 ...
- Spring中Quartz的最精简的配置
<bean id="parserJob4Pos" class="org.springframework.scheduling.quartz.JobDetailBea ...
- [Stanford 2011] MVC introduction
以下是课程笔记,仅供以后复习之便. 1.什么是MVC? (1) Model:如飞机激战的游戏中,太空中的飞船,什么机型,每个飞船有多少机枪,多少护甲,这些硬件组成是model. (2)Controll ...
- Search in Rotated Sorted Array I
Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you befo ...