-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create number container system algorithm #8808
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
Merged
cclauss
merged 10 commits into
TheAlgorithms:master
from
CaedenPH:create-number-container-system
Jun 8, 2023
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
29eb341
feat: Create number container system algorithm
CaedenPH 1eb5039
updating DIRECTORY.md
4743aca
chore: Fix failing tests
CaedenPH 3e0acbe
Merge branch 'create-number-container-system' of https://github.com/c…
CaedenPH 82e1a0b
Update other/number_container_system.py
CaedenPH c95797b
Update other/number_container_system.py
CaedenPH 9bf70fe
Update other/number_container_system.py
CaedenPH 2322153
chore: Add more tests
CaedenPH c6c021a
chore: Create binary_search_insert failing test
CaedenPH f9801ac
type: Update typehints to accept str, list and range
CaedenPH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
""" | ||
A number container system that uses binary search to | ||
delete and insert values into arrays with O(n logn) | ||
write times and O(1) read times. | ||
|
||
This container system holds integers at indexes. | ||
|
||
Further explained in this leetcode problem | ||
> https://leetcode.com/problems/minimum-cost-tree-from-leaf-values | ||
""" | ||
|
||
|
||
class NumberContainer: | ||
def __init__(self) -> None: | ||
# Holds number as the key and returns list of indexes where the number is | ||
# The list of indexes is a sorted array in ascending order | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.numbermap: dict[int, list[int]] = {} | ||
# Simply holds each index and it's number | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.indexmap: dict[int, int] = {} | ||
|
||
def binary_search_delete(self, array: list[int], item: int) -> list[int]: | ||
""" | ||
Removes the item from the array and returns | ||
the new array. | ||
|
||
>>> NumberContainer().binary_search_delete([1,2,3], 2) | ||
[1, 3] | ||
>>> NumberContainer().binary_search_delete([], 0) | ||
CaedenPH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Traceback (most recent call last): | ||
... | ||
ValueError: The item is not in the array, and therefore cannot be deleted | ||
""" | ||
low = 0 | ||
high = len(array) - 1 | ||
|
||
while low <= high: | ||
mid = (low + high) // 2 | ||
if array[mid] == item: | ||
array.pop(mid) | ||
return array | ||
elif array[mid] < item: | ||
low = mid + 1 | ||
else: | ||
high = mid - 1 | ||
raise ValueError( | ||
"The item is not in the array, and therefore cannot be deleted" | ||
) | ||
|
||
def binary_search_insert(self, array: list[int], index: int) -> list[int]: | ||
""" | ||
Inserts the index into the sorted array | ||
at the correct position | ||
|
||
>>> NumberContainer().binary_search_insert([1,2,3], 2) | ||
[1, 2, 2, 3] | ||
>>> NumberContainer().binary_search_insert([0,1,3], 2) | ||
[0, 1, 2, 3] | ||
""" | ||
low = 0 | ||
high = len(array) - 1 | ||
|
||
while low <= high: | ||
mid = (low + high) // 2 | ||
if array[mid] == index: | ||
# If the item already exists in the array, | ||
# insert it after the existing item | ||
array.insert(mid + 1, index) | ||
return array | ||
elif array[mid] < index: | ||
low = mid + 1 | ||
else: | ||
high = mid - 1 | ||
|
||
# If the item doesn't exist in the array, insert it at the appropriate position | ||
array.insert(low, index) | ||
return array | ||
|
||
def change(self, index: int, number: int) -> None: | ||
""" | ||
Changes (sets) the index as number | ||
|
||
>>> cont = NumberContainer() | ||
>>> cont.change(0, 10) | ||
>>> cont.change(0, 20) | ||
>>> cont.change(-1, 20) | ||
""" | ||
# Remove previous index | ||
if index in self.indexmap: | ||
n = self.indexmap[index] | ||
if len(self.numbermap[n]) == 1: | ||
del self.numbermap[n] | ||
else: | ||
self.numbermap[n] = self.binary_search_delete(self.numbermap[n], index) | ||
|
||
# Set new index | ||
self.indexmap[index] = number | ||
|
||
# Number not seen before or empty so insert number value | ||
if number not in self.numbermap: | ||
self.numbermap[number] = [index] | ||
|
||
# Here we need to perform a binary search insertion in order to insert | ||
# The item in the correct place | ||
else: | ||
self.numbermap[number] = self.binary_search_insert( | ||
self.numbermap[number], index | ||
) | ||
|
||
def find(self, number: int) -> int: | ||
""" | ||
Returns the smallest index where the number is. | ||
|
||
>>> cont = NumberContainer() | ||
>>> cont.find(10) | ||
-1 | ||
>>> cont.change(0, 10) | ||
>>> cont.find(10) | ||
0 | ||
>>> cont.change(0, 20) | ||
>>> cont.find(10) | ||
-1 | ||
>>> cont.find(20) | ||
0 | ||
""" | ||
# Simply return the 0th index (smallest) of the indexes found (or -1) | ||
return self.numbermap.get(number, [-1])[0] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.