当前位置:网站首页>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
边栏推荐
- Leetcode question brushing: String 06 (implement strstr())
- 亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践
- 传纸条【动态规划】
- 会计要素包括哪些内容
- MacOS環境下使用HomeBrew安裝[email protected]
- Leetcode: String 04 (reverse the words in the string)
- 宝藏又小众的覆盖物PBR多通道贴图素材网站分享
- leetcode刷题:字符串04(颠倒字符串中的单词)
- 经典Wide & Deep模型介绍及tensorflow 2代码实现
- Is there any risk in registering and opening an account for stock speculation? Is it safe?
猜你喜欢

Leetcode question brushing: String 02 (reverse string II)

AI intelligent matting tool - hair can be seen

Record a redis large key troubleshooting

茂莱光学科创板上市:拟募资4亿 范一与范浩兄弟为实控人

大龄程序员的一些出路

网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...

Hands on deep learning pytorch version 3 - Data Preprocessing

在Flutter中解析复杂的JSON

Vi/vim editor

Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country
随机推荐
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
360手机助手首家接入APP签名服务系统 助力隐私安全分发
Leetcode question brushing: String 02 (reverse string II)
About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)
Flutter 中 ValueNotifier<List<T>> 监听问题解决
The source code that everyone can understand (I) the overall architecture of ahooks
CVPR 2022 | 美团技术团队精选论文解读
YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again
MATLAB and MySQL database connection and data exchange (based on ODBC)
Listing of maolaiguang discipline on the Innovation Board: it is planned to raise 400million yuan. Fanyi and fanhao brothers are the actual controllers
How SAP Spartacus default routing configuration works
协同过滤进化版本NeuralCF及tensorflow2实现
Student information management system based on SSH Framework
leetcode刷题:哈希表08 (四数之和)
Module 5 operation
Homebrew installation in MacOS environment [email protected]
证券注册开户有没有什么风险?安全吗?
Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
Matrix calculator design for beginners of linear algebra based on Qt development
Leetcode: String 04 (reverse the words in the string)