Skip to content

Commit 7f1a552

Browse files
alexpantyukhingithub-actionspre-commit-ci[bot]
authored
add prefix sum (TheAlgorithms#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>
1 parent 3e1cb70 commit 7f1a552

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
## Data Structures
163163
* Arrays
164164
* [Permutations](data_structures/arrays/permutations.py)
165+
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
165166
* Binary Tree
166167
* [Avl Tree](data_structures/binary_tree/avl_tree.py)
167168
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)
@@ -174,6 +175,7 @@
174175
* [Diff Views Of Binary Tree](data_structures/binary_tree/diff_views_of_binary_tree.py)
175176
* [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py)
176177
* [Inorder Tree Traversal 2022](data_structures/binary_tree/inorder_tree_traversal_2022.py)
178+
* [Is Bst](data_structures/binary_tree/is_bst.py)
177179
* [Lazy Segment Tree](data_structures/binary_tree/lazy_segment_tree.py)
178180
* [Lowest Common Ancestor](data_structures/binary_tree/lowest_common_ancestor.py)
179181
* [Maximum Fenwick Tree](data_structures/binary_tree/maximum_fenwick_tree.py)

Diff for: data_structures/arrays/prefix_sum.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Author : Alexander Pantyukhin
3+
Date : November 3, 2022
4+
5+
Implement the class of prefix sum with useful functions based on it.
6+
7+
"""
8+
9+
10+
class PrefixSum:
11+
def __init__(self, array: list[int]) -> None:
12+
len_array = len(array)
13+
self.prefix_sum = [0] * len_array
14+
15+
if len_array > 0:
16+
self.prefix_sum[0] = array[0]
17+
18+
for i in range(1, len_array):
19+
self.prefix_sum[i] = self.prefix_sum[i - 1] + array[i]
20+
21+
def get_sum(self, start: int, end: int) -> int:
22+
"""
23+
The function returns the sum of array from the start to the end indexes.
24+
Runtime : O(1)
25+
Space: O(1)
26+
27+
>>> PrefixSum([1,2,3]).get_sum(0, 2)
28+
6
29+
>>> PrefixSum([1,2,3]).get_sum(1, 2)
30+
5
31+
>>> PrefixSum([1,2,3]).get_sum(2, 2)
32+
3
33+
>>> PrefixSum([1,2,3]).get_sum(2, 3)
34+
Traceback (most recent call last):
35+
...
36+
IndexError: list index out of range
37+
"""
38+
if start == 0:
39+
return self.prefix_sum[end]
40+
41+
return self.prefix_sum[end] - self.prefix_sum[start - 1]
42+
43+
def contains_sum(self, target_sum: int) -> bool:
44+
"""
45+
The function returns True if array contains the target_sum,
46+
False otherwise.
47+
48+
Runtime : O(n)
49+
Space: O(n)
50+
51+
>>> PrefixSum([1,2,3]).contains_sum(6)
52+
True
53+
>>> PrefixSum([1,2,3]).contains_sum(5)
54+
True
55+
>>> PrefixSum([1,2,3]).contains_sum(3)
56+
True
57+
>>> PrefixSum([1,2,3]).contains_sum(4)
58+
False
59+
>>> PrefixSum([1,2,3]).contains_sum(7)
60+
False
61+
>>> PrefixSum([1,-2,3]).contains_sum(2)
62+
True
63+
"""
64+
65+
sums = {0}
66+
for sum_item in self.prefix_sum:
67+
if sum_item - target_sum in sums:
68+
return True
69+
70+
sums.add(sum_item)
71+
72+
return False
73+
74+
75+
if __name__ == "__main__":
76+
import doctest
77+
78+
doctest.testmod()

0 commit comments

Comments
 (0)