我在这段代码中遇到了一个问题,如果矩阵的尺寸很小,它就能很好地工作,但是如果尺寸很大,例如50X50就不会响应。问题陈述中这些维度的预期输入高达1000X1000。有什么想法吗?
这是我的代码:1-
http://ideone.com/BJuLQh
答案 0 :(得分:0)
这是一个O(n^3)
算法: -
1. fix two columns (u,v) which are end of the rectangle.
2. take sum of row i within (u,v) in sum[i] in ascending order.
3. Use kadane's algorithm on sum[]
4. The solution will will give you two row values (i,j) and max sum.
5. So you current best rectangle is (i,u),(j,v).
6. Update global max sum if needed.
时间复杂度: -
Kadane's algorithm : O(N)
sum of row's i within (u,v) can be evaluated in O(N) using previous values.
Total : O(N^3) for all (u,v) pairs.