当前位置:网站首页>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;
}
};
边栏推荐
- [today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
- 【TensorRT】动态batch进行推理
- MySQL 日期【加号/+】条件筛选问题
- [today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
- C language file reading and writing
- Network protocol: TCP part2
- Proxy implements MySQL read / write separation
- 程序的编译和运行
- securecrt乱码解决方法[通俗易懂]
- Interpretation of filter execution sequence source code in sprigboot
猜你喜欢

Increase swap space

【TensorRT】动态batch进行推理
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day

移动web布局方法
![[noi simulation] string matching (suffix automata Sam, Mo team, block)](/img/db/3ccb00e78bba293acdae91ffa72a2c.png)
[noi simulation] string matching (suffix automata Sam, Mo team, block)

FanoutExchange交换机代码教程

Detailed explanation of document operation

Character function and string function (2)
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

JMeter - interface test
随机推荐
Arrow 之 Parquet
test
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
Step num problem
Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC
Force deduction ----- calculate the money of the force deduction bank
Behind every piece of information you collect, you can't live without TA
Detailed explanation of document operation
Mobile web layout method
第六章 修改规范(SPEC)类
JS作用域与作用域链
JS scope and scope chain
Online random coin tossing positive and negative statistical tool
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
How much memory does bitmap occupy in the development of IM instant messaging?
leetcode-6131:不可能得到的最短骰子序列
Difference Between Accuracy and Precision
Recommended books | essentials of industrial digital transformation: methods and Practice
Today's sleep quality record 75 points