Maximum Submatrix
Given matrix =
[
[1,3,-1],
[2,3,-2],
[-1,-2,-3]
]
return 9.
Explanation:
the submatrix with the largest possible sum is:
[
[1,2],
[2,3]
] def maxSubmatrix(self, matrix):
if not matrix or not matrix[0]:
return 0
m, n = len(matrix), len(matrix[0])
best = matrix[0][0]
for lo in xrange(m):
for hi in xrange(lo, m):
if lo == hi:
oneD = matrix[lo]
else:
oneD = map(sum, zip(oneD, matrix[hi]))
currentMax = oneD[0] #完全按一维数组来做
best = max(best, currentMax)
for n in oneD[1:]:
currentMax = max(n, currentMax + n)
best = max(best, currentMax)
return bestLast updated