-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2349. Design a Number Container System #1288
Comments
We need to design a number container system that allows efficient insertion or replacement of numbers at specific indices and retrieval of the smallest index for a given number. The solution must handle these operations efficiently even with a large number of indices and numbers. ApproachThe approach involves using two main data structures:
Steps:
Let's implement this solution in PHP: 2349. Design a Number Container System <?php
class NumberContainers {
private $indexToNumber;
private $numberToHeap;
/**
*/
function __construct() {
$this->indexToNumber = array();
$this->numberToHeap = array();
}
/**
* @param Integer $index
* @param Integer $number
* @return NULL
*/
function change($index, $number) {
// Check if the index already has a number and it's the same as the new number
if (isset($this->indexToNumber[$index]) && $this->indexToNumber[$index] === $number) {
return;
}
// Update the index's number
$this->indexToNumber[$index] = $number;
// Add the index to the new number's heap
if (!isset($this->numberToHeap[$number])) {
$this->numberToHeap[$number] = new SplMinHeap();
}
$this->numberToHeap[$number]->insert($index);
}
/**
* @param Integer $number
* @return Integer
*/
function find($number) {
if (!isset($this->numberToHeap[$number])) {
return -1;
}
$heap = $this->numberToHeap[$number];
while (!$heap->isEmpty()) {
$index = $heap->top();
// Check if the current number at this index matches the target number
if (isset($this->indexToNumber[$index]) && $this->indexToNumber[$index] === $number) {
return $index;
} else {
// Remove the invalid index from the heap
$heap->extract();
}
}
return -1;
}
}
// Example usage:
$nc = new NumberContainers();
echo $nc->find(10) . "\n"; // Output: -1
$nc->change(2, 10);
$nc->change(1, 10);
$nc->change(3, 10);
$nc->change(5, 10);
echo $nc->find(10) . "\n"; // Output: 1
$nc->change(1, 20);
echo $nc->find(10) . "\n"; // Output: 2
?> Explanation:
This approach ensures efficient handling of both operations, with the change operation in O(log n) time (due to heap insertion) and the find operation efficiently removing invalid indices in O(log n) time for each removal, ensuring optimal performance for large datasets. |
Discussed in #1287
Originally posted by mah-shamim February 8, 2025
Topics:
Hash Table
,Design
,Heap (Priority Queue)
,Ordered Set
Design a number container system that can do the following:
Implement the
NumberContainers
class:NumberContainers()
Initializes the number container system.void change(int index, int number)
Fills the container atindex
with thenumber
. If there is already anumber
at that index, replace it.int find(int number)
Returns the smallest index for the givennumber
, or-1
if there is no index that is filled bynumber
in the system.Example 1:
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
NumberContainers nc = new NumberContainers();
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
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.
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.
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.
Constraints:
1 <= index, number <= 109
105
calls will be made in total tochange
andfind
.Hint:
The text was updated successfully, but these errors were encountered: