2349. Design a Number Container System Medium

@problem@discussion
#Hash Table#Design#Heap (Priority Queue)#Ordered Set



1/**
2 * [2349] Design a Number Container System
3 *
4 * Design a number container system that can do the following:
5 * 
6 * 	Insert or Replace a number at the given index in the system.
7 * 	Return the smallest index for the given number in the system.
8 * 
9 * Implement the NumberContainers class:
10 * 
11 * 	NumberContainers() Initializes the number container system.
12 * 	void change(int index, int number) Fills the container at index with the number. If there is already a number at that index, replace it.
13 * 	int find(int number) Returns the smallest index for the given number, or -1 if there is no index that is filled by number in the system.
14 * 
15 *  
16 * Example 1:
17 * 
18 * Input
19 * ["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
20 * [[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
21 * Output
22 * [null, -1, null, null, null, null, 1, null, 2]
23 * Explanation
24 * NumberContainers nc = new NumberContainers();
25 * nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
26 * nc.change(2, 10); // Your container at index 2 will be filled with number 10.
27 * nc.change(1, 10); // Your container at index 1 will be filled with number 10.
28 * nc.change(3, 10); // Your container at index 3 will be filled with number 10.
29 * nc.change(5, 10); // Your container at index 5 will be filled with number 10.
30 * nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
31 * nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20. 
32 * nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	1 <= index, number <= 10^9
38 * 	At most 10^5 calls will be made in total to change and find.
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/design-a-number-container-system/
44// discuss: https://leetcode.com/problems/design-a-number-container-system/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48struct NumberContainers {
49        false
50    }
51
52
53/** 
54 * `&self` means the method takes an immutable reference.
55 * If you need a mutable reference, change it to `&mut self` instead.
56 */
57impl NumberContainers {
58
59    fn new() -> Self {
60        
61    }
62    
63    fn change(&self, index: i32, number: i32) {
64        
65    }
66    
67    fn find(&self, number: i32) -> i32 {
68        
69    }
70}
71
72/**
73 * Your NumberContainers object will be instantiated and called as such:
74 * let obj = NumberContainers::new();
75 * obj.change(index, number);
76 * let ret_2: i32 = obj.find(number);
77 */
78
79// submission codes end
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn test_2349() {
87    }
88}
89


Back
© 2025 bowen.ge All Rights Reserved.