FAANG · LeetCode #303
Range Sum Query — Immutable — three-stage drill
1The ProblemEasy
Range Sum Query — Immutable
Answer many sumRange(l, r) queries on a fixed array.
Examples
sumRange(1,3) on [2,4,1,5,3]→ 10sumRange(0,4)→ 15
constraintArray never changes (immutable); many queries.
2The PatternTIME O(n) · SPACE O(n)
Prefix Sum
Running cumulative totals turn range-sum questions into O(1) lookups.
Problems using this pattern
3The SolutionRESULT → O(1)
Step through it
P[0] = 0 (sum of nothing)
Step 1 / 9
Arrow keys to navigate · escape to reset