当前位置:网站首页>leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
2022-07-03 18:38: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
总结
双指针贪心去除最短板
边栏推荐
- Raft 日志复制
- Niuke monthly race 31 minus integer
- Sensor debugging process
- Torch learning notes (6) -- logistic regression model (self training)
- 204. Count prime
- How about the Moco model?
- 4. Load balancing and dynamic static separation
- 2022-2028 global scar care product industry research and trend analysis report
- Image 24 bits de profondeur à 8 bits de profondeur
- Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
猜你喜欢
How to analyze the rising and falling rules of London gold trend chart
The vscode code is automatically modified to a compliance code when it is formatted and saved
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
2022-2028 global petroleum pipe joint industry research and trend analysis report
Getting started with JDBC
CV in transformer learning notes (continuously updated)
2022-2028 global scar care product industry research and trend analysis report
Computer graduation design PHP campus address book telephone number inquiry system
虚拟机和开发板互Ping问题
Multifunctional web file manager filestash
随机推荐
How to analyze the rising and falling rules of London gold trend chart
PHP determines which constellation it belongs to today
The vscode code is automatically modified to a compliance code when it is formatted and saved
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
English语法_名词 - 分类
Reading a line from ifstream into a string variable
English語法_名詞 - 分類
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
What is the function of registering DLLs- What does registering a DLL do?
Class exercises
Sepconv (separable revolution) code recurrence
Computer graduation design PHP makeup sales Beauty shopping mall
English语法_形容词/副词3级 - 倍数表达
Day-27 database
2022-2028 global sepsis treatment drug industry research and trend analysis report
How to expand the capacity of golang slice slice
Golang string (string) and byte array ([]byte) are converted to each other
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
Change the single node of Postgres database into master-slave