1/55
Loading...
🌾🌾 Farm Harvest Problem
• Given a 3×3 farm harvest grid • Need to quickly find rectangular region sums • Ex: sum of region (0,0)~(1,1) = ?
🔒
Loading...
• Given a 3×3 farm harvest grid • Need to quickly find rectangular region sums • Ex: sum of region (0,0)~(1,1) = ?
You manage an m x n farm. Each cell contains the harvest yield for that area. Multiple queries are given, each asking for the total harvest in a rectangular region from (r1, c1) to (r2, c2). Use 2D prefix sum to answer each query in O(1).
grid = [[1,2,3],[4,5,6],[7,8,9]], query = (0,0,1,1)
12
Sum of region (0,0)~(1,1): 1 + 2 + 4 + 5 = 12
grid = [[1,2,3],[4,5,6],[7,8,9]], query = (1,1,2,2)
28
Sum of region (1,1)~(2,2): 5 + 6 + 8 + 9 = 28