当前位置:网站首页>leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
2022-07-03 18:39:00 【白速龍王的回眸】
分析
一開始雙指針指著一頭一尾
然後看看哪個小,就把它往中間移動(這樣做的目的是去除最短板,才有可能變大)
ac code
class Solution:
def maxArea(self, height: List[int]) -> int:
# double pointers
n = len(height)
l, r = 0, n - 1
ans = 0
while l < r:
ans = max(ans, min(height[l], height[r]) * (r - l))
# 貪心:去除最短板
if height[l] < height[r]:
l += 1
else:
r -= 1
return ans
總結
雙指針貪心去除最短板
边栏推荐
- [combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
- Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it
- Valentine's day, send you a little red flower~
- Recent learning experience
- Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
- 22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
- Caddy server agent
- Unity2018 to wechat games without pictures
- Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
- Mysql45 lecture learning notes (II)
猜你喜欢
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
What kind of experience is it when the Institute earns 20000 yuan a month?
2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
Unity webgl optimization
Recommend a simple browser tab
2022-2028 global sepsis treatment drug industry research and trend analysis report
Sensor 调试流程
Golang string (string) and byte array ([]byte) are converted to each other
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
Raft 日志复制
随机推荐
Computer graduation design PHP makeup sales Beauty shopping mall
Redis cache avalanche, penetration, breakdown
12、 Service management
How to analyze the rising and falling rules of London gold trend chart
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
Valentine's day, send you a little red flower~
Nodejs (01) - introductory tutorial
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
php-fpm的max_chindren的一些误区
After nohup NPM start &, close the shell window directly, and the process closes accordingly
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
English语法_名词 - 分类
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
How do microservices aggregate API documents? This wave of operation is too good
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
How to read the source code [debug and observe the source code]
[Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
Ping problem between virtual machine and development board