mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-21 08:42:03 +00:00
add prefix sum (#7959)
* add prefix sum * updating DIRECTORY.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
3e1cb70abf
commit
7f1a5521f4
|
@ -162,6 +162,7 @@
|
||||||
## Data Structures
|
## Data Structures
|
||||||
* Arrays
|
* Arrays
|
||||||
* [Permutations](data_structures/arrays/permutations.py)
|
* [Permutations](data_structures/arrays/permutations.py)
|
||||||
|
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
|
||||||
* Binary Tree
|
* Binary Tree
|
||||||
* [Avl Tree](data_structures/binary_tree/avl_tree.py)
|
* [Avl Tree](data_structures/binary_tree/avl_tree.py)
|
||||||
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)
|
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)
|
||||||
|
@ -174,6 +175,7 @@
|
||||||
* [Diff Views Of Binary Tree](data_structures/binary_tree/diff_views_of_binary_tree.py)
|
* [Diff Views Of Binary Tree](data_structures/binary_tree/diff_views_of_binary_tree.py)
|
||||||
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
|
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
|
||||||
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
|
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
|
||||||
|
* [Is Bst](data_structures/binary_tree/is_bst.py)
|
||||||
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
|
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
|
||||||
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
|
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
|
||||||
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)
|
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)
|
||||||
|
|
78
data_structures/arrays/prefix_sum.py
Normal file
78
data_structures/arrays/prefix_sum.py
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
"""
|
||||||
|
Author : Alexander Pantyukhin
|
||||||
|
Date : November 3, 2022
|
||||||
|
|
||||||
|
Implement the class of prefix sum with useful functions based on it.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class PrefixSum:
|
||||||
|
def __init__(self, array: list[int]) -> None:
|
||||||
|
len_array = len(array)
|
||||||
|
self.prefix_sum = [0] * len_array
|
||||||
|
|
||||||
|
if len_array > 0:
|
||||||
|
self.prefix_sum[0] = array[0]
|
||||||
|
|
||||||
|
for i in range(1, len_array):
|
||||||
|
self.prefix_sum[i] = self.prefix_sum[i - 1] + array[i]
|
||||||
|
|
||||||
|
def get_sum(self, start: int, end: int) -> int:
|
||||||
|
"""
|
||||||
|
The function returns the sum of array from the start to the end indexes.
|
||||||
|
Runtime : O(1)
|
||||||
|
Space: O(1)
|
||||||
|
|
||||||
|
>>> PrefixSum([1,2,3]).get_sum(0, 2)
|
||||||
|
6
|
||||||
|
>>> PrefixSum([1,2,3]).get_sum(1, 2)
|
||||||
|
5
|
||||||
|
>>> PrefixSum([1,2,3]).get_sum(2, 2)
|
||||||
|
3
|
||||||
|
>>> PrefixSum([1,2,3]).get_sum(2, 3)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
IndexError: list index out of range
|
||||||
|
"""
|
||||||
|
if start == 0:
|
||||||
|
return self.prefix_sum[end]
|
||||||
|
|
||||||
|
return self.prefix_sum[end] - self.prefix_sum[start - 1]
|
||||||
|
|
||||||
|
def contains_sum(self, target_sum: int) -> bool:
|
||||||
|
"""
|
||||||
|
The function returns True if array contains the target_sum,
|
||||||
|
False otherwise.
|
||||||
|
|
||||||
|
Runtime : O(n)
|
||||||
|
Space: O(n)
|
||||||
|
|
||||||
|
>>> PrefixSum([1,2,3]).contains_sum(6)
|
||||||
|
True
|
||||||
|
>>> PrefixSum([1,2,3]).contains_sum(5)
|
||||||
|
True
|
||||||
|
>>> PrefixSum([1,2,3]).contains_sum(3)
|
||||||
|
True
|
||||||
|
>>> PrefixSum([1,2,3]).contains_sum(4)
|
||||||
|
False
|
||||||
|
>>> PrefixSum([1,2,3]).contains_sum(7)
|
||||||
|
False
|
||||||
|
>>> PrefixSum([1,-2,3]).contains_sum(2)
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
|
||||||
|
sums = {0}
|
||||||
|
for sum_item in self.prefix_sum:
|
||||||
|
if sum_item - target_sum in sums:
|
||||||
|
return True
|
||||||
|
|
||||||
|
sums.add(sum_item)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user