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的更多相关文章

  1. 710. Random Pick with Blacklist - LeetCode

    Question 710. Random Pick with Blacklist Solution 题目大意:给一个N,表示一个范围[0,N),给一个黑名单列表blacklist,其中blacklis ...

  2. [LeetCode] Random Pick with Blacklist 带黑名单的随机选取

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

  3. [Swift]LeetCode710. 黑名单中的随机数 | Random Pick with Blacklist

    Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...

  4. [LeetCode] Random Pick with Weight 根据权重随机取点

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  5. LeetCode 528. Random Pick with Weight

    原题链接在这里:https://leetcode.com/problems/random-pick-with-weight/ 题目: Given an array w of positive inte ...

  6. 398. Random Pick Index - LeetCode

    Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...

  7. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  8. Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  9. Leetcode: Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

随机推荐

  1. [VS]VS2010如何使用Visual Studio Online在线服务管理团队资源(在线TFS)

    前言 Visual Studio Online,也就是以前的Team Foundation Service,从名字可以看出这是一个团队资源管理服务.在微软的云基础架构中运行,无需安装或配置任何服务器, ...

  2. Unity3D笔记 英保通六 角色控制器

    一.角色控制器 U3D有两种角色控制方式:Rigidbody刚体.角色控制器组件(胶囊体组件) 面试的题目中经常会遇到这个问题: CharacterController和Rigidbody的区别? 这 ...

  3. iOS Swift 实现图片点击缩放回弹动画

    效果就是下面这个样子: 思路借鉴的是MZTimerLabel,有想过做一个自定义的ImageView,但那样的话之前view用必须要改代码,索性就按照MZTimerLabel这个方式实现,简单易用,从 ...

  4. CONVERT(varchar(10), getdate(), 120 )中数字参数用法

    这是一个mssql数据库的函数,Convert函数的作用,是进行数据类型的转换.而您所问的这个convert(char(20),openDate,120)则是对日期字段,进行格式化转换成字符格式的函数 ...

  5. 2018牛客网暑期ACM多校训练营(第三场) H - Shuffle Cards - [splay伸展树][区间移动][区间反转]

    题目链接:https://www.nowcoder.com/acm/contest/141/C 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  6. ZOJ 3985 - String of CCPC - [字符串处理]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3985 题意: 给出一个长度为n的字符串,全部由'C'和'P'组成 ...

  7. IntelliJ IDEA 2018.3注册码

    修改hosts windows,打开C:/Windows/System32/drivers/etc/hosts linux打开 vi /etc/hosts 输入: 0.0.0.0 account.je ...

  8. Java applets A Java applet example

    https://en.wikipedia.org/wiki/Ajax_(programming) https://zh.wikipedia.org/wiki/AJAX Ajax (also AJAX; ...

  9. 洛谷P3166 数三角形 [CQOI2014] 数论

    正解:数论 解题报告: 传送门! 很久以前做的题了呢,,,回想方法还想了半天QAQ 然后写这题题解主要是因为看到了好像有很新颖的法子,就想着,学习一下趴,那学都学了不写博客多可惜 首先港下最常规的方法 ...

  10. PostGIS 快速入门(转)

    原文:http://live.osgeo.org/zh/quickstart/postgis_quickstart.html PostGIS 是 PostgreSQL 关系数据库的空间操作扩展.它为 ...