当前位置:网站首页>Leetcode-6130: designing digital container systems
Leetcode-6130: designing digital container systems
2022-07-25 20:38:00 【Chrysanthemum headed bat】
leetcode-6130: Design digital container system
subject
Design a digital container system , The following functions can be realized :
- Given the subscript in the system Insert perhaps Replace A number .
- return The minimum subscript of a given number in the system .
Please realize a NumberContainers class :
- NumberContainers() Initialize the digital container system .
- void change(int index, int number) In subscript index Fill in number . If the subscript index There are already numbers at , Then use number Replace this number .
- int find(int number) Returns the given number number The minimum subscript in the system . If there is no number , Then the return -1 .
Example :
Input :
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
Output :
[null, -1, null, null, null, null, 1, null, 2]
explain :
NumberContainers nc = new NumberContainers();
nc.find(10); // There are no numbers 10 , So back -1 .
nc.change(2, 10); // The subscript in the container is 2 Fill in the number 10 .
nc.change(1, 10); // The subscript in the container is 1 Fill in the number 10 .
nc.change(3, 10); // The subscript in the container is 3 Fill in the number 10 .
nc.change(5, 10); // The subscript in the container is 5 Fill in the number 10 .
nc.find(10); // Numbers 10 Where the subscript is 1 ,2 ,3 and 5 . Because the minimum subscript is 1 , So back 1 .
nc.change(1, 20); // The subscript in the container is 1 Fill in the number 20 . Be careful , Subscript 1 Before 10 , Now it is replaced by 20 .
nc.find(10); // Numbers 10 The subscript is 2 ,3 and 5 . The minimum subscript is 2 , So back 2 .

Problem solving
Method 1 : Hash map+ Ordered set set
establish Numbers —> Indexes Hash map, So when searching , You can find it directly
What if we get the smallest index ? You can use ordered sets set, that set.begin() The element pointed to is the smallest index .
So build unordered_map<int,set<int>> , Can stabilize O(1) Complexity complete search
class NumberContainers {
public:
unordered_map<int,int> index2num;// Indexes ---> Numbers
unordered_map<int,set<int>> num2index;// Numbers ---> Indexes
NumberContainers() {
}
void change(int index, int number) {
if(index2num.count(index)==0){
// Newly inserted index , Directly establish map Just go .
index2num[index]=number;
num2index[number].insert(index);
}else{
// If the index existed before
// Delete the old index first
int oldNum=index2num[index];
num2index[oldNum].erase(index);
if(num2index[oldNum].empty()){
num2index.erase(oldNum);
}
// Insert new index
index2num[index]=number;
num2index[number].insert(index);
}
}
int find(int number) {
if(num2index.count(number)){
return *num2index[number].begin();
}
return -1;
}
};
边栏推荐
- Chapter VI modified specification (SPEC) class
- [today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
- Remote—基本原理介绍
- Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
- Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- Formatdatetime explanation [easy to understand]
- [MCU] 51 MCU burning those things
- Arrow parquet
- Scan delete folder problems
猜你喜欢
![[leetcode] 28. Implement strstr ()](/img/87/0af2da458e53d31012d6535cc132bb.png)
[leetcode] 28. Implement strstr ()
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/60/09c5f44d64b96c6e4d57e5f426e4ed.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results

Introduction and construction of consul Registration Center

Docker 搭建 Redis Cluster集群
![Vulnhub | dc: 5 | [actual combat]](/img/c6/34117bbfb83ebdf9e619f4e4590661.png)
Vulnhub | dc: 5 | [actual combat]

Difference Between Accuracy and Precision

Has baozi ever played in the multi merchant system?

Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released

智能电子界桩自然保护区远程监控解决方案

Network protocol: TCP part2
随机推荐
Proxy implements MySQL read / write separation
QML combines qsqltablemodel to dynamically load data MVC "recommended collection"
Arrow parquet
RF、GBDT、XGboost特征选择方法「建议收藏」
leetcode-6126:设计食物评分系统
Docker 搭建 Redis Cluster集群
4everland storage node portal network design
TGA file format (waveform sound file format)
Go language go language built-in container
Aircraft PID control (rotor flight control)
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
Step num problem
[MCU] 51 MCU burning those things
Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
Dataframe first performs grouping operation and then combines output
The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
Working principle of radar water level gauge and precautions for installation and maintenance
leetcode-6129:全 0 子数组的数目
Online random coin tossing positive and negative statistical tool