方法1:
class Solution(object):
def maxCount(self, m, n, ops):
"""
:type m: int
:type n: int
:type ops: List[List[int]]
:rtype: int
"""
if not ops:
return m * n
else:
return min(op[0] for op in ops) * min(op[1] for op in ops)
Sol = Solution()
print Sol.maxCount(3,3[[2,2],[3,3]])
方法2:
def maxCount(self, R, C, ops):
if not ops: return R * C
X, Y = zip(*ops)
return min(X) * min(Y)
0,非常快速的算法
1,题目的具体要求
2,如果用所谓常规的方法来做,也就是生成M,对M进行操作,统计最多的数字的次数,这样的时间复杂度代价非常之高(好几个for),不可以这样做。
需要直接从题目的要求上入手。
3,从实质上来说,方法1和方法2的原理是基本一样的,只不过处理数据上有一点不同的地方。方法1是直接处理,而方法2用到了zip函数方法:
使用案例:
ops = [[1,2],[3,4],[5,6]]
print zip(*ops)
x,y = zip(*ops)
print x,y
输出:
>>>[(1, 3, 5), (2, 4, 6)]
>>>(1, 3, 5) (2, 4, 6)