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 ...
随机推荐
- Java秒杀简单设计三:数据封装类
上一篇https://www.cnblogs.com/taiguyiba/p/9828984.html 整合了数据库表和Dao层代码 这一篇继续设计数据封装类: 涉及到获取秒杀地址,查询,返回秒杀结果 ...
- ELK篇---------elasticsearch集群安装配置
说明: 本次ELK的基础配置如下: 虚拟机:vmware 11 系统:centos7.2 两台 IP:172.16.1.15/16 一.下载es wget https://download.elas ...
- 洞察行业领先者的前沿思想——第五届TOP100全球软件案例研究峰会精彩谢幕
(第五届TOP100summit开幕式现场) 12月09日-12日,由msup主办的第五届TOP100全球软件案例研究峰会(以下简称TOP100summit)在北京国家会议中心举行,作为互联网行业最有 ...
- 第四课(1)——MySQL体系结构
学习目标 一.MySQL体系结构 二.MySQL内存结构 三.MySQL文件结构 四.Innodb体系结构 MySQL体系结构 一.MySQL体系结构图 1.Mysql是由SQL接口,解析器,优化器, ...
- jeb 下载
jeb-1.5.201408040(full)_keygen_by_scz(20150725) http://scz.617.cn/ 修改jeb_wincon.bat 中java home 变量,然后 ...
- TCP 123=网络时间协议(NTP),Net Controller
TCP 123=网络时间协议(NTP),Net Controller
- iOS-Foundation框架—结构体(转载)
一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...
- python3爬虫-爬取新浪新闻首页所有新闻标题
准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...
- 对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解
对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解
- literallycanvas的简介
LiterallyCanvas是什么 Literally Canvas是一个可扩展的开源(BSD许可)HTML5绘图组件,可以用于网页中插入画图板,类似于windows自带的画图板.可以用可视化工具绘 ...