当前位置:网站首页>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;
}
};
边栏推荐
- leetcode-6127:优质数对的数目
- leetcode-114:二叉树展开为链表
- Difference Between Accuracy and Precision
- How to use buffer queue to realize high concurrent order business (glory Collection Edition)
- If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
- 智能电子界桩自然保护区远程监控解决方案
- 【TensorRT】动态batch进行推理
- DIY个人服务器(diy存储服务器)
- Compilation and operation of program
- MySQL date [plus sign / +] condition filtering problem
猜你喜欢

Struct, enum type and union

Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
![[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

How to realize reliable transmission with UDP?

Introduction and construction of consul Registration Center

leetcode-6129:全 0 子数组的数目

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

Mobile web layout method

火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布

Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
随机推荐
移动web布局方法
SecureCRT garbled code solution [easy to understand]
Network RTK UAV test [easy to understand]
Follow up of Arlo's thinking
leetcode-6125:相等行列对
[tensorrt] trtexec tool to engine
结构体,枚举类型与联合体
leetcode-6127:优质数对的数目
Kubernetes进阶部分学习笔记
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
"Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
How to realize reliable transmission with UDP?
Card link
[advanced mathematics] [3] Application of differential mean value theorem and derivative
[advanced mathematics] [6] differential calculus of multivariate functions
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
Mobile web layout method
Success factors of software R & D effectiveness measurement
每条你收藏的资讯背后,都离不开TA
JS scope and scope chain