Skip to content
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

Develop #1290

Merged
merged 102 commits into from
Feb 8, 2025
Merged

Develop #1290

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
acb6603
Automated commit 1
mah-shamim Feb 8, 2025
9a651dd
Automated commit rollback 1
mah-shamim Feb 8, 2025
3e0ca10
Automated commit 2
mah-shamim Feb 8, 2025
ec81791
Automated commit rollback 2
mah-shamim Feb 8, 2025
6957fa6
Automated commit 3
mah-shamim Feb 8, 2025
65472e7
Automated commit rollback 3
mah-shamim Feb 8, 2025
04008df
Automated commit 4
mah-shamim Feb 8, 2025
019d33b
Automated commit rollback 4
mah-shamim Feb 8, 2025
88e0fcf
Automated commit 5
mah-shamim Feb 8, 2025
25edfb1
Automated commit rollback 5
mah-shamim Feb 8, 2025
4ee77d1
Automated commit 6
mah-shamim Feb 8, 2025
48a6812
Automated commit rollback 6
mah-shamim Feb 8, 2025
9241ced
Automated commit 7
mah-shamim Feb 8, 2025
69a3bb0
Automated commit rollback 7
mah-shamim Feb 8, 2025
2542c80
Automated commit 8
mah-shamim Feb 8, 2025
2b28c57
Automated commit rollback 8
mah-shamim Feb 8, 2025
9f86afc
Automated commit 9
mah-shamim Feb 8, 2025
c264177
Automated commit rollback 9
mah-shamim Feb 8, 2025
8a3f87f
Automated commit 10
mah-shamim Feb 8, 2025
23974d8
Automated commit rollback 10
mah-shamim Feb 8, 2025
0b01a49
Automated commit 11
mah-shamim Feb 8, 2025
987cf30
Automated commit rollback 11
mah-shamim Feb 8, 2025
c6cd01e
Automated commit 12
mah-shamim Feb 8, 2025
c5622fa
Automated commit rollback 12
mah-shamim Feb 8, 2025
73e27fb
Automated commit 13
mah-shamim Feb 8, 2025
cf9fa99
Automated commit rollback 13
mah-shamim Feb 8, 2025
fd49ae5
Automated commit 14
mah-shamim Feb 8, 2025
a341541
Automated commit rollback 14
mah-shamim Feb 8, 2025
7e9f9f5
Automated commit 15
mah-shamim Feb 8, 2025
043c5c8
Automated commit rollback 15
mah-shamim Feb 8, 2025
e1b3ed4
Automated commit 16
mah-shamim Feb 8, 2025
3c5b4e4
Automated commit rollback 16
mah-shamim Feb 8, 2025
5f1b393
Automated commit 17
mah-shamim Feb 8, 2025
6eded05
Automated commit rollback 17
mah-shamim Feb 8, 2025
aafd281
Automated commit 18
mah-shamim Feb 8, 2025
8b39e14
Automated commit rollback 18
mah-shamim Feb 8, 2025
212b441
Automated commit 19
mah-shamim Feb 8, 2025
1373f99
Automated commit rollback 19
mah-shamim Feb 8, 2025
e37e0ae
Automated commit 20
mah-shamim Feb 8, 2025
2342a88
Automated commit rollback 20
mah-shamim Feb 8, 2025
61189f6
Automated commit 21
mah-shamim Feb 8, 2025
b0d045b
Automated commit rollback 21
mah-shamim Feb 8, 2025
2231c21
Automated commit 22
mah-shamim Feb 8, 2025
4e437f0
Automated commit rollback 22
mah-shamim Feb 8, 2025
9e14d8b
Automated commit 23
mah-shamim Feb 8, 2025
1c17a69
Automated commit rollback 23
mah-shamim Feb 8, 2025
0060a15
Automated commit 24
mah-shamim Feb 8, 2025
62150fe
Automated commit rollback 24
mah-shamim Feb 8, 2025
1365b3e
Automated commit 25
mah-shamim Feb 8, 2025
fd3839b
Automated commit rollback 25
mah-shamim Feb 8, 2025
5d4ae2a
Automated commit 26
mah-shamim Feb 8, 2025
f6be2d8
Automated commit rollback 26
mah-shamim Feb 8, 2025
d75d13f
Automated commit 27
mah-shamim Feb 8, 2025
f5be2ac
Automated commit rollback 27
mah-shamim Feb 8, 2025
53d76e3
Automated commit 28
mah-shamim Feb 8, 2025
458911b
Automated commit rollback 28
mah-shamim Feb 8, 2025
b00f1d1
Automated commit 29
mah-shamim Feb 8, 2025
1ff69b2
Automated commit rollback 29
mah-shamim Feb 8, 2025
8276b9f
Automated commit 30
mah-shamim Feb 8, 2025
3960523
Automated commit rollback 30
mah-shamim Feb 8, 2025
c256eb8
Automated commit 31
mah-shamim Feb 8, 2025
f68580e
Automated commit rollback 31
mah-shamim Feb 8, 2025
549660d
Automated commit 32
mah-shamim Feb 8, 2025
90383f8
Automated commit rollback 32
mah-shamim Feb 8, 2025
d7e2d72
Automated commit 33
mah-shamim Feb 8, 2025
8df6c72
Automated commit rollback 33
mah-shamim Feb 8, 2025
58dba68
Automated commit 34
mah-shamim Feb 8, 2025
91f5a89
Automated commit rollback 34
mah-shamim Feb 8, 2025
a845ef6
Automated commit 35
mah-shamim Feb 8, 2025
6a37afd
Automated commit rollback 35
mah-shamim Feb 8, 2025
2db9995
Automated commit 36
mah-shamim Feb 8, 2025
33ea0bc
Automated commit rollback 36
mah-shamim Feb 8, 2025
ce488e1
Automated commit 37
mah-shamim Feb 8, 2025
4907581
Automated commit rollback 37
mah-shamim Feb 8, 2025
43a5b5f
Automated commit 38
mah-shamim Feb 8, 2025
ed526de
Automated commit rollback 38
mah-shamim Feb 8, 2025
4b3cf2b
Automated commit 39
mah-shamim Feb 8, 2025
63780f0
Automated commit rollback 39
mah-shamim Feb 8, 2025
9eb589d
Automated commit 40
mah-shamim Feb 8, 2025
9806405
Automated commit rollback 40
mah-shamim Feb 8, 2025
d42c280
Automated commit 41
mah-shamim Feb 8, 2025
075c2fb
Automated commit rollback 41
mah-shamim Feb 8, 2025
0806961
Automated commit 42
mah-shamim Feb 8, 2025
80b8149
Automated commit rollback 42
mah-shamim Feb 8, 2025
ac215c1
Automated commit 43
mah-shamim Feb 8, 2025
8da88b9
Automated commit rollback 43
mah-shamim Feb 8, 2025
8095802
Automated commit 44
mah-shamim Feb 8, 2025
4703a50
Automated commit rollback 44
mah-shamim Feb 8, 2025
c056603
Automated commit 45
mah-shamim Feb 8, 2025
5d735bf
Automated commit rollback 45
mah-shamim Feb 8, 2025
e331692
Automated commit 46
mah-shamim Feb 8, 2025
d53a876
Automated commit rollback 46
mah-shamim Feb 8, 2025
dbac4bc
Automated commit 47
mah-shamim Feb 8, 2025
a1dbce6
Automated commit rollback 47
mah-shamim Feb 8, 2025
336d1ea
Automated commit 48
mah-shamim Feb 8, 2025
98e0a4d
Automated commit rollback 48
mah-shamim Feb 8, 2025
42dd125
Automated commit 49
mah-shamim Feb 8, 2025
c4fb92c
Automated commit rollback 49
mah-shamim Feb 8, 2025
2209f39
Automated commit 50
mah-shamim Feb 8, 2025
336dcf1
Automated commit rollback 50
mah-shamim Feb 8, 2025
9ad15f9
#1286, #1287 leetcode problems 002349-design-a-number-container-syste…
mah-shamim Feb 8, 2025
c855e29
Merge pull request #1289 from mah-shamim/002349-design-a-number-conta…
mah-shamim Feb 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions algorithms/002349-design-a-number-container-system/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
2349\. Design a Number Container System

**Difficulty:** Medium

**Topics:** `Hash Table`, `Design`, `Heap (Priority Queue)`, `Ordered Set`

Design a number container system that can do the following:

- **Insert** or **Replace** a number at the given index in the system.
- **Return** the smallest index for the given number in the system.

Implement the `NumberContainers` class:

- `NumberContainers()` Initializes the number container system.
- `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.
- `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.


**Example 1:**

- **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]
- **Explanation:**
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:**

- <code>1 <= index, number <= 10<sup>9</sup></code>
- At most <code>10<sup>5</sup></code> calls will be made in total to `change` and `find`.


**Hint:**
1. Use a hash table to efficiently map each number to all of its indices in the container and to map each index to their current number.
2. In addition, you can use ordered set to store all of the indices for each number to solve the find method. Do not forget to update the ordered set according to the change method.



**Solution:**

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.

### Approach
The approach involves using two main data structures:
1. **Hash Map (Associative Array) for Index to Number Mapping**: This helps in quickly determining the current number at any given index.
2. **Min-Heap for Number to Indices Mapping**: This allows efficient retrieval of the smallest index for a given number. Each number maps to a min-heap that stores all indices where the number is currently located, ensuring that the smallest index can be retrieved in O(1) time.

**Steps:**
1. **Initialization**: Create two hash maps. One to store the current number at each index and another to map each number to a min-heap of indices.
2. **Change Operation**: When inserting or replacing a number at an index:
- Check if the index already has a number. If it does, update the index to the new number.
- Add the index to the min-heap corresponding to the new number.
3. **Find Operation**: For retrieving the smallest index for a given number:
- Check if the number exists in the hash map. If not, return -1.
- Continuously check the top of the min-heap for the number, removing any invalid indices (indices that no longer hold the number) until a valid index is found or the heap is empty.

Let's implement this solution in PHP: **[2349. Design a Number Container System](https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002349-design-a-number-container-system/solution.php)**

```php
<?php
class NumberContainers {
/**
*/
function __construct() {
...
...
...
/**
* go to ./solution.php
*/
}

/**
* @param Integer $index
* @param Integer $number
* @return NULL
*/
function change($index, $number) {
...
...
...
/**
* go to ./solution.php
*/
}

/**
* @param Integer $number
* @return Integer
*/
function find($number) {
...
...
...
/**
* go to ./solution.php
*/
}
}

// 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:

- **Initialization**: The constructor initializes two associative arrays to keep track of the current number at each index and the min-heap for each number.
- **Change Operation**: The `change` method updates the index with the new number. If the index previously held another number, it is automatically handled by checking and updating the hash map. The index is then added to the new number's min-heap.
- **Find Operation**: The `find` method retrieves the smallest index for a given number by checking the top of the corresponding min-heap. If the index at the top is invalid (no longer holds the number), it is removed from the heap, and the next smallest index is checked until a valid one is found or the heap is empty.

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.

**Contact Links**

If you found this series helpful, please consider giving the **[repository](https://github.com/mah-shamim/leet-code-in-php)** a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

- **[LinkedIn](https://www.linkedin.com/in/arifulhaque/)**
- **[GitHub](https://github.com/mah-shamim)**
60 changes: 60 additions & 0 deletions algorithms/002349-design-a-number-container-system/solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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;
}
}

/**
* Your NumberContainers object will be instantiated and called as such:
* $obj = NumberContainers();
* $obj->change($index, $number);
* $ret_2 = $obj->find($number);
*/