sum@arbitrary subMatrix, immutable ints #Rahul

Q: Given an N-array of immutable ints, write an efficient sumMatrixSum(le,ri,top,bot). Assume top-most and left-most indices are zeros.

My solution: pre-populate in O(NN) a shadow-matrix s, where at each location s(row, col) is the sum of submatrix(0,col, 0, bot).

Query will be O(1) :

s(0,ri, bot) + s(le-1, top-1) – s(le-1, bot) – s(r, top-1)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s