当前位置:网站首页>leetcode:710. Random numbers in the blacklist [mapping thinking]
leetcode:710. Random numbers in the blacklist [mapping thinking]
2022-06-26 21:43:00 【Review of the white speed Dragon King】

analysis
Let's give a boundary first bound We hope that all blacklists can be mapped to bound( Include ) The position after
If the blacklist had been bound Back , We just put it in black in
Then traverse the rest of the blacklist , If you find that , It's in bound front
We from bound Start with the value of , Start traversing backwards one by one , Find the first non blacklist location w
Then make b2w[b] = w This completes the mapping from blacklist to whitelist
And then finally use a random one [0, bound - 1] Number of numbers
If there is no mapping , That means it's a white list ; If there is an insinuation , Find the corresponding white list
ac code
class Solution:
def __init__(self, n: int, blacklist: List[int]):
m = len(blacklist)
self.bound = w = n - m
# hypothesis bound There are blacklists in the back , There are white lists ahead
black = {
b for b in blacklist if b >= self.bound}
self.b2w = {
}
for b in blacklist:
# If you encounter a blacklist
if b < self.bound:
# Find the space in the back
while w in black:
w += 1
# Put the current blacklist mapping to White list of corresponding positions
self.b2w[b] = w
# Next position
w += 1
#print(self.b2w)
def pick(self) -> int:
x = randrange(self.bound) # Excluding the right endpoint , from 0 Start , In steps of 1
#x = randint(0, self.bound - 1)
return self.b2w.get(x, x) # If it's not a blacklist , Just choose yourself ; Otherwise, select the mapped white list
summary
Blacklist mapping method , Make according to the former bound A random case
You can find the value of the white list mapped by the blacklist
边栏推荐
- Cause analysis of 12 MySQL slow queries
- 龙芯中科科创板上市:市值357亿 成国产CPU第一股
- In 2022, where will the medium and light-weight games go?
- DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
- 「连续学习Continual learning, CL」最新2022研究综述
- 基于启发式搜索的一字棋
- 经典Wide & Deep模型介绍及tensorflow 2代码实现
- Student information management system based on SSH Framework
- DLA模型(分类模型+改进版分割模型) + 可变形卷积
- 网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...
猜你喜欢

VB.net类库,获取屏幕内鼠标下的颜色(进阶——3)

BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结

基于QT实现简单的连连看小游戏

花店橱窗布置【动态规划】

Chapter 2 construction of self defined corpus
![[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination](/img/f9/68b5b5ce21f4f851439fa061b477c9.jpg)
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination

Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews

YOLOv6:又快又准的目標檢測框架開源啦

The latest 2022 research review of "continuous learning, CL"

Redis + guava local cache API combination, performance burst!
随机推荐
矩阵求导及其链式法则
[protobuf] some pits brought by protobuf upgrade
Android mediacodec hard coded H264 file (four), ByteDance Android interview
DLA模型(分类模型+改进版分割模型) + 可变形卷积
MATLAB与Mysql数据库连接并数据交换(基于ODBC)
The source code that everyone can understand (I) the overall architecture of ahooks
经典Wide & Deep模型介绍及tensorflow 2代码实现
AI intelligent matting tool - hair can be seen
360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
十大券商注册开户有没有什么风险?安全吗?
大家都能看得懂的源码(一)ahooks 整体架构篇
Application and Optimization Practice of 100 million level monthly live national karaoke feed service in Tencent cloud mongodb
leetcode:141. 环形链表【哈希表 + 快慢指针】
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
API管理之利剑 -- Eolink
AI智能抠图工具--头发丝都可见
y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
中金财富开户安全吗?我想开户炒股。
这个算BUG吗?乱填的字母是否可以关闭
Vi/vim editor