➕ Prefix Sum & Difference Arrays #3
tpemeja
started this conversation in
Algorithm Checklist
Replies: 1 comment
|
1D : 2D: Create bigger matrix (n+1, m+1) Donc pour calculer B[i+1, j+1] il faut prendre tout ce qui est en haut, tout ce qui est a gauche et enlever B[i, j] puis que compter en double et ajouter la value de A[i,j] qui n'est dans aucune des deux. En ensuite quand on veut la somme de r1, c1 à r2, c2, il faut prendre B[r2+1, c2+1] - B[r2+1, c1] - B[r1, c2+1] + B[r1 c1]
https://newsletter.francofernando.com/p/range-queries
https://archive.mcpt.ca/cpt-lessons/advanced/2d-prefix-sum-array/
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment



Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Patterns
Choose
Details
✅ Prefix Sum (1D)
Summary (Key points):
Details
Concept recap:
Generic template:
Case 1: Subarray Sum Equals K
Case 2: Subarray Sums Divisible by K
Case 3: Continuous Subarray Sum
Case 4: Find Pivot Index
Case 5: Range Sum Query – Immutable
Case 6: Sum of Absolute Differences in a Sorted Array
Case 7: Minimum Average Difference
Pitfalls and tips:
Complexity summary:
✅ Prefix Sum (2D)
Summary (Key points):
Details
Concept recap:
Generic template:
Case 1: Range Sum Query 2D – Immutable
Case 2: Number of Submatrices That Sum to Target
Case 3: Max Sum of Rectangle No Larger Than K
Case 4: Matrix Block Sum
Pitfalls and tips:
Complexity summary:
✅ Difference Arrays
Summary (Key points):
Details
Concept recap (1D):
Generic template (1D):
2D difference (rectangle updates):
Case 1: Range Addition
Case 2: Corporate Flight Bookings
Case 3: Car Pooling
Case 4: Shifting Letters II
Pitfalls and tips:
Complexity summary:
All reactions