当前位置:网站首页>[leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set
[leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set
2022-07-18 08:49:00 【alone_ yue】
List of articles
Leetcode 6113. The smallest number in an infinite set
1. Problem description

2. Solution
Solution 1 :
Hash Responsible for the marker in the set true, Absent is false, And in popSmallest and addBack In the process of , Maintain a minimum value dynamically
class SmallestInfiniteSet {
private Boolean[] Hash;
private int Min;
public SmallestInfiniteSet() {
Hash = new Boolean[2001];
Arrays.fill(Hash, true);
Min = 1;
}
public int popSmallest() {
int t = Min;
Hash[Min] = false;
int left = Min+1;
while(Hash[left]==false){
left++;
}
Min = left;
return t;
}
public void addBack(int num) {
if(Hash[num]==false){
Hash[num] = true;
if(num<Min) Min = num;
}
}
}
Solution 2 :
Because it is necessary to judge whether it is in the set , use HashSet Sure o(1) Achieve , then PriorityQueue Responsible for taking the minimum value
class SmallestInfiniteSet {
private PriorityQueue<Integer> queue = new PriorityQueue<>();
private HashSet<Integer> set = new HashSet<>();
public SmallestInfiniteSet() {
int cnt = 1000;
for(int i=1;i<=cnt;i++){
set.add(i);
queue.add(i);
}
}
public int popSmallest() {
int ans = queue.remove();
set.remove(ans);
return ans;
}
public void addBack(int num) {
if(!set.contains(num)){
set.add(num);
queue.add(num);
}
}
}
边栏推荐
- Yesdev: easily collaborate on every project
- Qt(六)数值与字符串转换
- Application of spectral normalization in Gan with LSTM as discriminator (tensorflow2)
- 290 pages 110000 words digital agriculture rural project planning and construction scheme 2022
- 【集训DAY2】Torch bearer【暴力】【DFS】
- [in depth study of 4g/5g/6g special topic -37]: urllc-8 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -2- network architecture, delay analysis and
- 【集训DAY1】Maximum benefit【离散化】【贪心】
- 当使用single-branch clone仓库后,如何添加跟踪的远程分支?
- go 语言学习笔记(1)
- 鸿湖万联致远开发板正式合入OpenHarmony主干
猜你喜欢
随机推荐
微信小程序激活账号时,提示“此帐号已激活,请使用帐号密码直接登录”
IDEA的修改背景照片and使用技巧
【集训DAY3】Delete【模拟】
OpenCV 教程 01:简介与安装,图片与视频的基本操作
Measurement of conductive potential of thin film copper foil
DNS attack protection principle
base64和blob对图片的压缩
测试开发的六大能力
290页11万字数字农业农村项目规划建设方案2022
In the process of emergency response, what is a good way to find files created on a certain date?
Uniapp custom header component
(codeforce1699)A&B (构造)
Replace specific characters / text improvements in word with POI
全面解读数据中台,让企业实现数字化转型
数据库:使用WHERE语句进行检索(头歌)
蓝帽杯2022初赛电子取证
(2021牛客多校五)D-Double Strings(乘法原理+动态规划)
Test the speed characteristics of optical flow sensor
Da Vinci pro's FPGA learning notes 6.2 -- VCs and Verdi development hummingbird e203
Go1.18 upgrade function - fuzzy test | go language from scratch








