Design a system that stores numbers in "containers" (indexed slots) and supports two main operations:
1.change(index, number):
Set the container at the specified index to the given number. If a container already has a number, replace it with the new number.
-1
.change(index, number)
, the system stores number
at that index. If the container already held a number, it is replaced.find(number)
, the system searches for all containers containing that number and returns the smallest index among them. If none is found, it returns -1
.Input:
["NumberContainers", "change", "change", "find", "change", "find"] [[], [2, 10], [5, 10], [10], [2, 20], [10]]
Expected Output:
[null, null, null, 2, null, 5]
Step-by-Step Explanation:
Initialization:
Operation: change(2, 10)
Operation: change(5, 10)
Operation: find(10)
2
Operation: change(2, 20)
Operation: find(10)
5
Input:
["NumberContainers", "change", "find", "change", "change", "find", "change", "find", "find"] [[], [3, 15], [15], [7, 15], [3, 20], [15], [7, 30], [15], [20]]
Expected Output:
[null, null, 3, null, null, 7, null, -1, 3]
Step-by-Step Explanation:
Initialization:
Operation: change(3, 15)
Operation: find(15)
3
Operation: change(7, 15)
Operation: change(3, 20)
Operation: find(15)
7
Operation: change(7, 30)
Operation: find(15)
-1
Operation: find(20)
3
1 <= index, number <= 10^9
10^5
calls combined to the change
and find
operations.https://leetcode.com/problems/design-a-number-container-system/description
Loading component...
Loading component...