当前位置:网站首页>【Leetcode周赛--哈希表模拟】6113.无限集中的最小数字
【Leetcode周赛--哈希表模拟】6113.无限集中的最小数字
2022-07-15 18:17:00 【alone_yue】
Leetcode 6113.无限集中的最小数字
1.问题描述

2.解决方案
解法一:
Hash负责在集合内的标位true,不在的为false,并且在popSmallest和addBack的过程中,动态维护一个最小值
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;
}
}
}
解法二:
因为要判断是不是在集合中,用HashSet可以o(1)做到,然后PriorityQueue负责取最小值
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);
}
}
}
边栏推荐
- Windows10 reset MySQL user password
- uniapp自定义头部组件
- QT (II) UI control introduction and optional tree control demonstration
- Test the speed characteristics of optical flow sensor
- Meituan side: @transactional principle and common pits?
- MYSQL和 ORACLE 的常见区别(一)
- Li Mu hands on deep learning V2 target detection data set
- Torch in pytoch Sort() and torch Argsort() function parsing
- 测试开发的六大能力
- Torch in pytoch Analysis of nonzero() function
猜你喜欢

VxWorks environment construction and learning

"Smart factory" goes online, breaking the traditional factory digital transformation

Develop command line tools

Broadcast mechanism in pytoch

Qt(四)使用代码与UI文件混合开发

Several common methods of database table query in SAP ABAP system

对接企业微信,客户关系管理也可以很简单!

A-F codeworks round 806 (div.4) A-F problem solution and code

Selenium uses' chromedriver 'executable needs to be in path Error reporting information

Logic of archives | holonomic distinction and examples
随机推荐
第三讲:spfa求最短路
Go1.18 upgrade function - fuzzy test | go language from scratch
Swift 自定义Subscript
C语言:数的阶乘递归实现
Vscode [because scripts are not allowed to run on this system]
开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?
Several common methods of database table query in SAP ABAP system
How do I open an account with tongdaxin? Is it safe to open a stock account by mobile phone?
SAP ABAP 系统进行数据库表查询的几种常用方法的试读版
DNS attack protection principle
90% of people have never used Microsoft!
开发命令行工具
如何清理你的电子邮件订阅者名单以改善电子邮件营销
当使用single-branch clone仓库后,如何添加跟踪的远程分支?
Torch in pytoch Analysis of nonzero() function
【每日一题】558. 四叉树交集
#夏日挑战赛# HarmonyOS 实现一个手绘板
记录记录记录
Harmonic cloud classroom | agile development process and project practice sharing
傅立叶变换的物理意义