Skip to content

Commit 60651f2

Browse files
Fix
1 parent 619c905 commit 60651f2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

dynamic_programming/range_sum_query.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
3737
Algorithm:
3838
So, first we calculate the prefix sum (dp) of the array.
39-
The prefix sum of the index i is the sum of all elements indexed from 0 to i (inclusive).
39+
The prefix sum of the index i is the sum of all elements indexed
40+
from 0 to i (inclusive).
4041
The prefix sum of the index i is the prefix sum of index (i - 1) + the current element.
4142
So, the state of the dp is dp[i] = dp[i - 1] + a[i].
4243
@@ -51,7 +52,8 @@
5152
If the query was l = 3, r = 4,
5253
the answer would be 6 + 3 = 9 but this would require O(r - l + 1) time ≈ O(N) time
5354
54-
If we use prefix sums we can find it in O(1) by using the formula prefix[r] - prefix[l - 1].
55+
If we use prefix sums we can find it in O(1) by using the formula
56+
prefix[r] - prefix[l - 1].
5557
This formula works because prefix[r] is the sum of elements from [0, r]
5658
and prefix[l - 1] is the sum of elements from [0, l - 1],
5759
so if we do prefix[r] - prefix[l - 1] it will be

0 commit comments

Comments
 (0)