710 Random Pick with Blacklist
1. 问题
给定一个黑名单,包含[0, N)的一些数,从[0, N)之间的非黑名单数中随机采样一个值。
2. 思路
字典映射
(1)计算黑名单数的长度,记作B,因为已经排除掉了B个元素,所以最后是从N-B个数中采样。
(2)可以维护一个字典,表示从[0, N-B)到[0, N)之间的映射。
(3)这样就可以每次采样从[0, N-B)之间取,采样后将值映射回[0, N)。
(4)然而这么做爆内存了(MemoryError),因为N的最大长度为10亿,B的最大长度为10万,N-B特别大。
时间复杂度:O(N-B),空间复杂度:O(N-B),B表示blacklist的长度
(方法二)字典映射
(1)实际上我们不需要维护[0, N-B)中每个数的映射,我们只需要考虑[0, N-B)中的blacklist元素,因为只有这些元素才发生冲突。
(2)考虑换一种映射方法,因为是从[0, N-B)中采样,我们只需要考虑把[0, N-B)中的blacklist元素映射到[N-B, N)即可。
(3)而[N-B, N)中的blacklist元素是不需要(也不能)被映射的。我们只需要考虑那些[N-B, N)中不在blacklist中的元素,保证这些元素被映射就好了。
(4)所以我们只需要遍历[N-B, N)里面的B个元素,如果元素不在blacklist中,就建立一个映射,让blacklist中的元素(按顺序递增)指向它。
(5)这里判断元素是否在blacklist时,使用set效率会更高(一开始使用的list导致了超时)。映射时还是使用list的blacklist,因为要按顺序来映射。
时间复杂度:O(B * logB),空间复杂度:O(B),B表示blacklist的长度
3. 代码
字典映射
class Solution(object):
def __init__(self, N, blacklist):
"""
:type N: int
:type blacklist: List[int]
"""
B = len(blacklist)
dic = {}
offset = 0
for i in range(N-B):
if(i+offset not in blacklist):
dic[i] = i + offset
else:
offset += 1
dic[i] = i + offset
self.dic = dic
self.N = N-B
def pick(self):
"""
:rtype: int
"""
i = random.randint(0,self.N-1)
return self.dic[i]
(方法二)字典映射
class Solution(object):
def __init__(self, N, blacklist):
blacklist.sort()
blacklist_set = set(blacklist)
self.dic = {}
self.M = N - len(blacklist)
j = 0
for i in range(self.M, N):
if i not in blacklist_set:
self.dic[blacklist[j]] = i
j += 1
def pick(self):
i = random.randint(0,self.M - 1)
return self.dic[i] if i in self.dic else i
710 Random Pick with Blacklist的更多相关文章
- 710. Random Pick with Blacklist - LeetCode
Question 710. Random Pick with Blacklist Solution 题目大意:给一个N,表示一个范围[0,N),给一个黑名单列表blacklist,其中blacklis ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...
- [Swift]LeetCode710. 黑名单中的随机数 | 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 528. Random Pick with Weight
原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...
- 398. Random Pick Index - LeetCode
Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- Leetcode: Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
随机推荐
- myeclipse2016破解过程
1.安装myeclipse2016 CI 7就不介绍了..只需要注意最后安装完成后取消对号.不要立即运行myeclipse2016. 2.下载破解版工具. 到以下网址下载破解工具:http://dow ...
- 【CF839E】Mother of Dragons 折半状压
[CF839E]Mother of Dragons 题意:给你一张n个点,m条边的无向图.你有k点能量,你可以把能量分配到任意一些点上,每个点分到的能量可以是一个非负实数.定义总能量为:对于所有边&l ...
- [转]理解Linux的处理器负载均值
[转自]http://www.mike.org.cn/articles/understanding-of-linux-processor-load-average/ 你可能对于Linux的负载均值(l ...
- Print or Cout an Unsigned Char Variable 打印无符号字符
在C++中,unsigned char用来表示一个字节,也就是8位大小的值,那么我们如何来打印出其值呢,用cout直接打印会乱码,我们可以通过下面两种方法来打印: cout << stat ...
- IIS7.5 配置虚拟目录的经历
好多网站为了不带上什么端口号所有就建立虚拟目录的试来使用80端口 iis6设置都没有问题可到了7.5碰到了点问题 原来7.5上有个:添加应用程序和添加虚拟目录.还有个转换为应用程序 直接把网站建成虚拟 ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu4513完美队形II manacher
吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要 ...
- Win_Server_2008 安装 Oracle_11g EM时上载EM资料失败
此问题本人也遇到过.在网上找到了解决方案.下部分引用IT PUB. 安装oracle11g 64位.创建数据库到快结束的时候,报告说EM无法创建.emca_2010_06_13_11_05_36.lo ...
- python3学习笔记(9)_closure
#python 学习笔记 2017/07/13 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #从高阶函数的定义,我们可以知道,把函数作为参数的函数, ...
- RSA原理说明
长度,建议至少1024.模数n(常取默认65537)两边都要用. 指数e,和n一起就是公钥. 指数d,和n一起就是私钥. 质数p和q用于生成密钥对,然后就丢弃不公开. 一.密钥对的生成步骤 1.随机选 ...