我在这段代码中有一个问题,如果矩阵的尺寸很小,它可以很好地工作,但是如果尺寸很大,例如50X50,它就不会响应。问题说明中这些尺寸的预期输入最多为1000X1000。任何的想法?
这是我的代码:http : 1-
//ideone.com/BJuLQh
这是一个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.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句