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 ...
随机推荐
- ElasticSearch概述及Linux下的单机ElasticSearch安装
原文链接:http://blog.csdn.net/w12345_ww/article/details/52182264 这两天在项目中要涉及到ElasticSearch的使用,就上网去搜索了一些这方 ...
- [工具] Textify – 复制不可能的窗口内容[Win]
Textify 是一款 Windows 下的小工具,能够复制那些平时无法复制的内容,比如错误提示.菜单按钮文字等等,只需要按下快捷键就可以随意复制,俗称复制不可能. http://rammichael ...
- linux上jar包的运行
指定目录: #!/bin/bash source /etc/profile log() { echo `date +[%Y-%m-%d" "%H:%M:%S]` $1 } log ...
- java使用AES256解密
网上关于java用AES加密解密的文章有很多,我这里只用到解密(加密是服务器那边做^_^),所以更简洁一些: public class AES256Utils { private static fin ...
- vue--使用定时器的问题
https://blog.csdn.net/ywl570717586/article/details/79963162
- Ajax做列表无限加载和Ajax做二级下拉选项
//栏目Ajax做加载 public function ajaxlist(){ //echo "http://www.域名.com/index.php?a=Index&c=Index ...
- poj1182 食物链【并查集-好题!】
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两 ...
- mysql跨库联表查询
首先要了解database与instance区别,见<MySQL中的实例.数据库关系简介> 跨库分为同一个instance下的跨库和不同instance下的跨库. 一.同一个MySQL实例 ...
- 关于Python的集合set
网上那么多说创建集合的语句是: >>>a=set([1,2,3]) python 3.6.3,你们真的能运行吗? 我这里报: Traceback (most recent call ...
- linux 启动过程关键点
Freeing init memory: 4568K init... Freeing init memory 后,就是开始init进程