当前位置:网站首页>Container containing the most water
Container containing the most water
2022-06-10 18:04:00 【Elite cadre flaw light】
original edition :

Personal translation :
Given an array of integers , Enumerate , The index is X Axis , Values are Y Axis , Find out the maximum area of two of them .
Start with your own ideas and operations .
Design a two-layer loop , Set up a maximum area s1=0, Each number is traversed backwards from itself , Find the maximum area replacement s1, The code is as follows :
class Solution:
def maxArea(self, height: List[int]) -> int:
carry=0
for i in range(len(height)):
for j in range(i,len(height)):
cantake=(j-i)*min(height[i],height[j])
carry=max(cantake,carry)
return carryresult : Failure , Code timeout .
This double-layer loop is equivalent to traversing the list once for each number , Too much work .
right key :
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
ans = 0
while l < r:
area = min(height[l], height[r]) * (r - l)
ans = max(ans, area)
if height[l] <= height[r]:
l += 1
else:
r -= 1
return ans
Use double pointer , Point to both ends of the list , Move to the middle . Use while You can avoid setting boundary values , At the same time, it is obviously more concise than double loop operation .
边栏推荐
猜你喜欢
随机推荐
【AXI】解读AXI协议双向握手机制的原理
美学心得(第二百三十七集) 罗国正
Open source project PM how to design official website
红色垂直左侧边菜单导航代码
安全感
Wireshark学习笔记(一)常用功能案例和技巧
Analysis of transfer Refund Scheme in e-commerce industry
AOE网关键路径
聊聊远程办公那些事儿,参与征文领稿费拿大奖!
Abbexa低样本量鸡溶菌酶 C (LYZ) ELISA 试剂盒
js模糊阴影跟随动画js特效插件
Summary of vim common commands
系统需要把所有文件扫描一遍,并尝试识别视频的封面
Abbexa丙烯酰胺-PEG-NHS说明书
Some views on the current CIM (bim+gis) industry
训练时添加进度条的库--tqdm
分享我做Dotnet9博客网站时积累的一些资料
2022版IDEA图形界面GUI乱码解决方法超详细简单版
PMP考生,深圳2022年6月PMP考试地点有这些
苹果期待的「无密码时代」,真能实现吗?








