【题目】
示例:
输入: [1,8,6,2,5,4,8,3,7]
输出: 49
【思路】
- 先将list排序
- 然后从最小y值元素开始,此时的元素线找到离它最远的线得到x,将y*x添加到ans
- 遍历完所有元素,返回max(ans)
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
h = list(sorted(height))
length = len(h)
n = [j for j in range(length)]
ans = []
#print(h,length)
for i in range(length-1):
if i>0 and h[i] == h[i-1]:
height[x] = []
x = height.index(h[i])
xl = x - n[0]
xr = n[-1] - x
#print(x,xl,xr)
if xl >= xr:
ans.append(xl*h[i])
else:
ans.append(xr*h[i])
n.remove(x)
#print(height, n, ans)
return max(ans)
height = [1,6,6,2,5,4,8,3,7]
#height = [3,1,3,4]
print(Solution().maxArea(height))
【总结】
嘿嘿,中等难度的题目完全靠自己做出来了,但是呃…代码耗时和内存消耗数据惨不忍睹,等之后学习一下别人的做法,改进一下。