当前位置:网站首页>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 .
边栏推荐
- LeetCode 255. 验证前序遍历序列二叉搜索树*
- High number_ Chapter 6 infinite series__ Absolute convergence_ Conditional convergence
- 系统需要把所有文件扫描一遍,并尝试识别视频的封面
- 玩轉Pytorch的Function類
- 为什么宇宙会将最大速度限制在光速
- 红色垂直左侧边菜单导航代码
- pands pd. Detailed parsing of dataframe() function
- OpenJudge NOI 1.13 15:求序列中的众数
- 掌握高性能计算前,我们先了解一下它的历史
- Noise line h5js effect realized by canvas
猜你喜欢

JS special effect of canvas divergent particle H5 animation

LeetCode树经典题目(一)

IP总结(TCP/IP卷1和卷2)

蓝桥杯_挑选子串_组合数学_乘法原理_ / 尺取法

Canvas大火燃烧h5动画js特效

传统企业在进行信息化升级的过程中,如何做好信息化顶层设计

True thesis of information system project manager in the first half of 2022

canvas发散的粒子h5动画js特效

高数_第6章无穷级数__正项级数的性质

Why 0.1+0.2=0.3000000000000004
随机推荐
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
True thesis of information system project manager in the first half of 2022
mmdetection之model构建
pands pd.DataFrame()函数详细解析
领导提拔你的原因,只有这点最真实,其他都是瞎扯!
MMdetection之build_optimizer模块解读
蓝桥杯_糊涂人寄信_递归
matplotlib plt.text()的具体用法——画图时给图中的点加标签
LeetCode 255. 验证前序遍历序列二叉搜索树*
Unity stepping on the pit record: if you inherit monobehavior, the constructor of the class may be called multiple times by unity. Do not initialize the constructor
分享这位大神的WPF界面设计系列视频
Leetcode 875. Coco, who likes bananas
待办事项桌面插件,办公族的桌面好帮手
LeetCode树经典题目(一)
Noise line h5js effect realized by canvas
关于cmake和gcc的安装的记录
AOE网关键路径
Vim常用命令总结
The short ticket hypothesis: finding sparse, trainable neural networks
High number_ Chapter 6 infinite series__ Properties of positive series