当前位置:网站首页>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;
}
};
边栏推荐
- Apache MINA框架「建议收藏」
- Apache Mina framework "suggestions collection"
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- [today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
- [advanced mathematics] [6] differential calculus of multivariate functions
- leetcode-6126:设计食物评分系统
- FanoutExchange交换机代码教程
- 【高等数学】【3】微分中值定理与导数的应用
- [today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
- SecureCRT garbled code solution [easy to understand]
猜你喜欢

Card link
![Vulnhub | dc: 5 | [actual combat]](/img/c6/34117bbfb83ebdf9e619f4e4590661.png)
Vulnhub | dc: 5 | [actual combat]
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions
![[MCU] 51 MCU burning those things](/img/fa/8f11ef64a18114365c084fff7d39f6.png)
[MCU] 51 MCU burning those things
![[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now

Kubernetes进阶部分学习笔记

Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference

Fanoutexchange switch code tutorial

Key network protocols in tcp/ip four layer model
随机推荐
Formatdatetime explanation [easy to understand]
【高等数学】【5】定积分及应用
Scan delete folder problems
leetcode-919:完全二叉树插入器
103. (cesium chapter) cesium honeycomb diagram (square)
KEGG通路的从属/注释信息如何获取
[advanced mathematics] [8] differential equation
Has baozi ever played in the multi merchant system?
【TensorRT】动态batch进行推理
How much memory does bitmap occupy in the development of IM instant messaging?
JS作用域与作用域链
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
Vulnhub | dc: 6 | [actual combat]
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
Apache Mina framework "suggestions collection"
If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
[leetcode] 28. Implement strstr ()
Character function and string function (2)
Working principle of radar water level gauge and precautions for installation and maintenance
TGA file format (waveform sound file format)