当前位置:网站首页>The container with the most water
The container with the most water
2020-11-08 10:30:00 【http://www.ubytovani-jihlava.cz】
Original published in :

Today is the weekend , Look at G An interview question from the company :
seek max{|i-j|*min{a[i], a[j]}} Value , among a It's an array of positive integers ,i and j The interval is [0, n-1].
This is actually leetcode Medium “ The container with the most water ”, as follows :

Lu xun said : Violence can solve all problems .
Hushi said : The problem that violence can solve , It's not a problem .
because i and j The possibility of a limited combination , So the brute force algorithm gets the result , But failed to pass the interview .
Do you use dynamic programming ? It seems that dynamic planning is not good either .

![]()
We can use double pointers , Point to the head and the tail respectively , Calculate the water content . And search in the middle , Try to find out more possibilities for holding water .
The barrel theory tells us : For two definite water baffles , The amount of water is determined by the short board .
therefore , When searching in the middle , Move the pointer from the short board to the middle , It is possible to produce a greater water holding value .
This is a central conclusion .
As for the code , It's simple , Take a look :
func maxArea(a []int) int {
n := len(a)
if n < 2 {
return 0
}
if n == 2 {
return min(a[1], a[0])
}
max := min(a[n - 1], a[0]) * (n - 1)
i := 0
j := n - 1
for i < j {
if a[i] < a[j] {
i++
}else {
j--
}
area := min(a[i], a[j]) * (j - i)
if area > max {
max = area
}
}
return max
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
result :

The algorithm can pass the interview normally , I wish you all the best offer.
Have a nice weekend .
版权声明
本文为[osc_1ajf1srl]所创,转载请带上原文链接,感谢
边栏推荐
- 数据科学面试应关注的6个要点
- Flink的sink实战之一:初探
- PCIe 枚举过程
- IQKeyboardManager 源代码看看
- Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
- Analysis of ArrayList source code
- Is there a big difference between i5 1135g7 and i51035g1? Which is better?
- YGC troubleshooting, let me rise again!
- More than 50 object detection datasets from different industries
- Mate 40系列发布 搭载华为运动健康服务带来健康数字生活
猜你喜欢

双向LSTM在时间序列异常值检测的应用

PDMS cutting software

蓝牙2.4G产品日本MIC认证的测试要求

新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务

【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)

C语言I博客作业03

python学习 day1——基础学习

NOIP 2012 提高组 复赛 第一天 第二题 国王游戏 game 数学推导 AC代码(高精度 低精度 乘 除 比较)+60代码(long long)+20分代码(全排列+深搜dfs)

Px4 adds new applications
![[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter](/img/3b/00bc81122d330c9d59909994e61027.jpg)
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
随机推荐
i5 1135g7和i5 1035g1参数对比区别大吗? 哪个好
laravel8更新之速率限制改进
C language I blog assignment 03
vivoS7e和vivoS7的区别 哪个更值得入手
PDMS cutting software
VC + + specified directory file output by time
Istio流量管理--Ingress Gateway
技术人员该如何接手一个复杂的系统?
AMD Zen3首发评测:频率超5GHz,IPC提升不止19%,这次真的Yes了 - 知乎
仅用六种字符来完成Hello World,你能做到吗?
Flink's sink: a preliminary study
python 循环区分(while循环和for循环)
Japan PSE certification
“1024”征文活动结果新鲜出炉!快来看看是否榜上有名?~~
[summary series] technical system of Internet server: high performance database index
Spotify是如何推动数据驱动决策的?
M 端软件产品设计思虑札记 - 知乎
ASP.NET A complete solution based on exception handling in MVC
TCP协议如何确保可靠传输
5G+AR出圈,中国移动咪咕成第33届中国电影金鸡奖全程战略合作伙伴