Please would anyone suggest a dynamic programming approach to solve the SPOJ problem "A STANDARD PROBLEM", link:- http://www.spoj.com/problems/ASTDPROB/
Problem statement: Given a boolean matrix of size NXM .Answer for all Q queries of type int low, high, finds the largest area sub rectangle having only Zeros and lying between rows numbered low and high.
1 ≤ N, M ≤ 1000
1 ≤ Q ≤ 10^6
I need an O(n^2) or O(n^2 * log n) dp algorithm.
Up till my approach goes like this
- I precompute the sides of maximum sub rectangle starting each cell (i,j) in almost O(n^2) time using DP.
- store the answer for each query in a grid ans[M][M] (currently my doing in O(n^3)=around 10^9 atomic operations in 1s which is impossible)
- then answer all queries in O(1).
Please suggest any optimization for 2nd step?
Anyone with more efficient approach,please share that also.
Thanks in advance.