classSolution: defmaxArea(self, height): length = len(height) area = 0 i = 0 j = length - 1 while i < j: x = j - i y = min(height[i],height[j]) area = max(area,x * y) if height[i] <= height[j]: i += 1 else: j -= 1 return area
(二)暴力破解
双层for循环遍历。
缺点:非常耗时
时间复杂度:O($n^2$) n是数组的长度
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11
classSolution: defmaxArea(self, height): length = len(height) max_area = 0 for i inrange(length): for j inrange(i+1,length): x = j - i y = min(height[i],height[j]) area = x * y max_area = max(max_area,area) return max_area