-
- Notifications
You must be signed in to change notification settings - Fork 48.7k
Add: Matrix Prefix Sum #10841
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
Open
invincible04 wants to merge 56 commits into TheAlgorithms:master Choose a base branch from invincible04:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Add: Matrix Prefix Sum #10841
Changes from 10 commits
Commits
Show all changes
56 commits Select commit Hold shift + click to select a range
35c6c7e
Add: Matrix Prefix Sum
invincible04 a8603ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b21b10d
Changes made in Matrix Prefix Sum
invincible04 322e45a
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 89d32bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2cee13c
Changes made in Matrix Prefix Sum
invincible04 4e2e56e
Changes made in Matrix Prefix Sum
invincible04 de886cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 336adf2
Changes made in Matrix Prefix Sum
invincible04 28d9cfd
Changes made in Matrix Prefix Sum
invincible04 b43f475
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 37718c7
Changes made in Matrix Prefix Sum
invincible04 e5ee235
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 e9403b2
Changes made in Matrix Prefix Sum
invincible04 7d7e2e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f987823
Changes made in Matrix Prefix Sum
invincible04 c0e18be
Changes made in Matrix Prefix Sum
invincible04 4ed2d72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 157c744
Changes made in Matrix Prefix Sum
invincible04 6e5c84c
Changes made in Matrix Prefix Sum
invincible04 5c3a01f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6b718c5
Changes made in Matrix Prefix Sum
invincible04 2c78030
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 11aa213
Changes made in Matrix Prefix Sum
invincible04 3ab1ea6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 97d1414
Changes made in Matrix Prefix Sum
invincible04 14000b3
Changes made in Matrix Prefix Sum
invincible04 0853c35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b01f637
Changes made in Matrix Prefix Sum
invincible04 13d806e
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 778df87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b568f9d
Changes made in Matrix Prefix Sum
invincible04 b03426e
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 aba2ef2
Changes made in Matrix Prefix Sum
invincible04 6280be4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 896f3a9
Changes made in Matrix Prefix Sum
invincible04 abe5fb0
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 01e6e1c
Changes made in Matrix Prefix Sum
invincible04 27022eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0d8007d
Changes made in Matrix Prefix Sum
invincible04 bc5276d
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 fbc4fa7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b9311e9
Changes made in Matrix Prefix Sum
invincible04 14e51db
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 9da4957
Add: Distinct Subsequences
invincible04 b2f0756
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 61a2824
Changes made in Distinct Subsequences
invincible04 ff23cb8
Changes made in Distinct Subsequences
invincible04 3329261
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fb935b2
Changes made in Distinct Subsequences
invincible04 b7e51c9
Merge branch 'master' of https://github.com/aayushsoni4/Python
invincible04 aaf1c3b
Changes made in Distinct Subsequences
invincible04 e268227
Changes made in Distinct Subsequences
invincible04 6ce845a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7cb9ca2
Removed Distinct Subsequences
invincible04 2eda0a9
Removed Distinct Subsequences
invincible04 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 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. | ||
""" | ||
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] | ||
| ||
# Calculate the prefix sum for the top-left cell. | ||
prefix_sum[0][0] = matrix[0][0] | ||
| ||
# Calculate cumulative sums for the first row. | ||
for i in range(1, cols): | ||
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): | ||
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 | ||
# - The cell to the left of it | ||
# - Subtracting the overlapping cell | ||
# - Adding the value from the original matrix | ||
prefix_sum[i][j] = ( | ||
prefix_sum[i - 1][j] + | ||
prefix_sum[i][j - 1] - | ||
prefix_sum[i - 1][j - 1] + | ||
matrix[i][j] | ||
) | ||
| ||
return prefix_sum | ||
| ||
def display_matrix(matrix: list[list[int]]) -> None: | ||
invincible04 marked this conversation as resolved. Show resolved Hide resolved invincible04 marked this conversation as resolved. Show resolved Hide resolved invincible04 marked this conversation as resolved. Show resolved Hide resolved invincible04 marked this conversation as resolved. Show resolved Hide resolved invincible04 marked this conversation as resolved. Show resolved Hide resolved invincible04 marked this conversation as resolved. Show resolved Hide resolved invincible04 marked this conversation as resolved. Show resolved Hide resolved invincible04 marked this conversation as resolved. Show resolved Hide resolved | ||
""" | ||
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(map(str, row)) | ||
| ||
if __name__ == "__main__": | ||
matrix = [ | ||
[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) |
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.