classSolution(object):defmaxSumSubmatrix(self,matrix,k):""" :type matrix: List[List[int]] :type k: int :rtype: int """m=len(matrix)n=len(matrix[0])ifmelse0# 并不知道行和列哪个大M=max(m,n)N=min(m,n)ans=Noneforxinrange(N):sums=[0]*Mforyinrange(x,N):# num 为[x->N, 0->M]这个矩阵中的和slist,num=[],0forzinrange(M):# 不知道行和列哪个大sums[z]+=matrix[z][y]ifm>nelsematrix[y][z]num+=sums[z]ifnum<=k:ans=max(ans,num)i=bisect.bisect_left(slist,num-k)ifi!=len(slist):ans=max(ans,num-slist[i])bisect.insort(slist,num)returnansor0