Skip to content
Open
Changes from 7 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
35c6c7e
Add: Matrix Prefix Sum
invincible04 Oct 23, 2023
a8603ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b21b10d
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
322e45a
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
89d32bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
2cee13c
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
4e2e56e
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
de886cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
336adf2
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
28d9cfd
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
b43f475
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
37718c7
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
e5ee235
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
e9403b2
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
7d7e2e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
f987823
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
c0e18be
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
4ed2d72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
157c744
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
6e5c84c
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
5c3a01f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
6b718c5
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
2c78030
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
11aa213
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
3ab1ea6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
97d1414
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
14000b3
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
0853c35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b01f637
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
13d806e
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
778df87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b568f9d
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
b03426e
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
aba2ef2
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
6280be4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
896f3a9
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
abe5fb0
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
01e6e1c
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
27022eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
0d8007d
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
bc5276d
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
fbc4fa7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
b9311e9
Changes made in Matrix Prefix Sum
invincible04 Oct 23, 2023
14e51db
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
9da4957
Add: Distinct Subsequences
invincible04 Oct 23, 2023
b2f0756
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
61a2824
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
ff23cb8
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
3329261
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
fb935b2
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
b7e51c9
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 Oct 23, 2023
aaf1c3b
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
e268227
Changes made in Distinct Subsequences
invincible04 Oct 23, 2023
6ce845a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2023
7cb9ca2
Removed Distinct Subsequences
invincible04 Oct 23, 2023
2eda0a9
Removed Distinct Subsequences
invincible04 Oct 23, 2023
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
79 changes: 79 additions & 0 deletions matrix/matrix_prefix_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Python Program to find prefix sum of a 2D array


def calculate_prefix_sum(matrix: list[list[int]]) -> list[list[int]]:
"""
Calculate the prefix sum of a 2D matrix.

Prefix sum is a technique used to efficiently calculate the cumulative sum of subarrays in a 2D array.
The idea is to compute a new array where each element represents the sum of all elements in the original array
up to that position.

Prefix Sum Formula:
prefix_sum[i][j] = prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + matrix[i][j]

:param matrix: A 2D matrix.
:return: A matrix containing the prefix sums.

>>> calculate_prefix_sum([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
[[1, 2, 3], [2, 4, 6], [3, 6, 9]]

>>> calculate_prefix_sum([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
[[1, 3, 6], [5, 12, 21], [12, 27, 45]]
"""
rows = len(matrix)
cols = len(matrix[0])

# Initialize the prefix sum matrix with zeros, with the same dimensions as the original matrix.
prefix_sum = [[0 for _ in range(cols)] for _ in range(rows)]

# The prefix sum at the top-left corner is the same as the original matrix value.
prefix_sum[0][0] = matrix[0][0]

# Calculate cumulative sums for the first row.
for i in range(1, cols):
# The value in the first row is the sum of the previous value in the same row
# and the value from the original matrix at the current column.
prefix_sum[0][i] = prefix_sum[0][i - 1] + matrix[0][i]

# Calculate cumulative sums for the first column.
for i in range(1, rows):
# The value in the first column is the sum of the previous value in the same column
# and the value from the original matrix at the current row.
prefix_sum[i][0] = prefix_sum[i - 1][0] + matrix[i][0]

# Update the values in the cells using the general formula.
for i in range(1, rows):
for j in range(1, cols):
# The value in each cell is the sum of:
# - The cell above it (prefix_sum[i-1][j])
# - The cell to the left of it (prefix_sum[i][j-1])
# - Subtracting the overlapping cell (top-left: prefix_sum[i-1][j-1])
# - Adding the value from the original matrix (matrix[i][j])
prefix_sum[i][j] = (
prefix_sum[i - 1][j] # Sum of values above
+ prefix_sum[i][j - 1] # Sum of values to the left
- prefix_sum[i - 1][j - 1] # Subtract the overlapping cell
+ matrix[i][j] # Add the value from the original matrix
)

return prefix_sum

def display_matrix(matrix) -> None:
"""
Display a 2D matrix.

:param matrix: A 2D matrix.
"""
for row in matrix:
# Join the elements of each row with spaces and print the result.
print(" ".join(str(cell) for cell in row))


if __name__ == "__main__":
a = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
# Calculate the prefix sum of the 2D matrix
prefix_sum_matrix = calculate_prefix_sum(matrix)

# Display the prefix sum matrix
display_matrix(prefix_sum_matrix)