标签:mat += python lang for app 固定 max range
1,借助最大连续子序列和来取得最大子矩阵
def MaxSubArray(nums):
n = len(nums)
dp = []
dp.append(nums[0])
mymax = dp[0]
startidx = 0
endidx = 0
for i in range(1, n):
if dp[i-1] + nums[i] <= nums[i] and dp[i-1] + nums[i] > 0:
# print "startidx:%d" % i
startidx = i
dp.append(max(dp[i-1] + nums[i], nums[i]))
if dp[i] > mymax:
# print "endidx:%d" % i
endidx = i
mymax = max(dp[i], mymax)
# print mymax
return mymax, startidx, endidx
def MaxSubMatrix(mymatrix):
row = len(mymatrix)
col = len(mymatrix[0])
# 循环外层
# 固定外层i
# 循环内层j
# 找出该矩阵中最大的 子矩阵
row_min,row_max,col_min,col_max = (0,0,0,0)
mymax = 0
for row_i in range(row):
# print row_i
for row_j in range(row_i, row):
# print row_j
numlist = []
for col_i in range(col):
# print col_i
num = 0
for row_tmp in range(row_i, row_j):
num += mymatrix[row_tmp][col_i]
numlist.append(num)
tmpmax,startidx,endidx = MaxSubArray(numlist)
if tmpmax > mymax:
mymax = tmpmax
row_min = row_i
row_max = row_j
col_min = startidx
col_max = endidx
print "mymax:%s,row_min:%s,row_max:%s,tartidx:%s,endidx:%s" % (mymax,row_min,row_max,startidx,endidx)
标签:mat += python lang for app 固定 max range
原文地址:https://www.cnblogs.com/liyangsBlog/p/13690775.html