# leetcode

leetcode解题得的源码，解题思路在代码的注释里。

## Brute-force/暴力法

#
# @lc app=leetcode.cn id=11 lang=python3
#
# [11] 盛最多水的容器
#

# @lc code=start
from typing import List

class Solution:
def __init__(self):
self.maxContent = 0

def force(self, height: List[int]) -> int:
# print(len(height))
# get the last one & del it
last = height.pop()
for key,value in enumerate(height):
# Short board effect, get the minimm value between the last one & current value
tmp = min(last,value)*(len(height)-key)
self.maxContent = max(tmp,self.maxContent)
# print(self.maxContent)
if len(height) == 1:
return self.maxContent
else:
#Iterativly call self.force() with new params height
return(self.force(height))

def maxArea(self, height: List[int]) -> int:
return {
0 :lambda height :self.force(height),
}[0](height)
# @lc code=end

if __name__ == "__main__":
solve = Solution()
print(solve.maxArea([1,8,6,2,5,4,8,3,7]))

• 42 / 50 个通过测试用例
• 状态：超出时间限制

• time complexity ：$O(n^{2})$ ,计算所有$\frac{n(n-1)}{2}$ 种高度组合的面积
• space complexity ：$O(1)$,使用恒定的self.maxContent空间

## Double-pointer/双指针法

#
# @lc app=leetcode.cn id=11 lang=python3
#
# [11] 盛最多水的容器
#

# @lc code=start
from typing import List

class Solution:
def __init__(self):
self.maxContent = 0

def burte_force(self, height: List[int]) -> int:
# print(len(height))
# get the last one & del it
last = height.pop()
for key,value in enumerate(height):
# Short board effect, get the minimm value between the last one & current value
tmp = min(last,value)*(len(height)-key)
self.maxContent = max(tmp,self.maxContent)
# print(self.maxContent)
if len(height) == 1:
return self.maxContent
else:
#Iterativly call self.burte_force() with new params height
return(self.burte_force(height))

def double_pointer(self, height: List[int]) -> int:
i, j= 0, len(height) - 1
while i < j:
# the pointers approach eacher other, their distance becomes smaller, so the only when the short one approach the long one the area can be larger.
if height[i] < height[j]:
self.maxContent = max(self.maxContent, height[i] * (j - i))
# the left pointer move right
i += 1
else:
self.maxContent = max(self.maxContent, height[j] * (j - i))
# the right pointer move left
j -= 1
return self.maxContent
def maxArea(self, height: List[int]) -> int:
return {
0 :lambda height :self.burte_force(height),
1 :lambda height :self.double_pointer(height),
}[1](height)
# @lc code=end

if __name__ == "__main__":
solve = Solution()
print(solve.maxArea([1,8,6,2,5,4,8,3,7]))

Accepted

• 50/50 cases passed (148 ms)
• Your runtime beats 66.95 % of python3 submissions
• Your memory usage beats 50.48 % of python3 submissions (14.6 MB)

• time complexity ： $O(n)$ ,双指针遍历一次底边宽度$N$。
• space complexity ：$O(1)$ ,使用恒定的self.maxContent空间

148 ms ?? passed ??

class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
L = len(height) - 1
m1 = 0
for i in range(L+1):
d1 = d2 = 0
for j1 in range(0, i):
if height[j1]>=height[i]:
d1 = i-j1
break
for j2 in range(L, i, -1):
if height[j2]>=height[i]:
d2 = j2-i
break
m = height[i]*max([d1, d2])
if m > m1:
m1 = m
return m1

0:00:00.000013 (我的)
0:00:00.000067(你的)

from datetime import datetime
solve = Solution()
start = datetime.now()
for i in range(10000):
result = solve.maxArea([1,8,6,2,5,4,8,3,7])
stop = datetime.now()
print((stop-start)/10000)
print(result)
6个月前 评论

6个月前 评论

62

12

43

46