当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Ulab 1.0.0 release
- 解决Safari浏览器下载文件文件名称乱码的问题
- Deeplight Technology Bluetooth protocol SRRC certification services
- 笔试面试题目:求缺失的最小正整数
- Cloud alibabab notes come out, the whole network detailed explanation only this one hand is slow
- Is software testing training class easy to find a job
- 个人目前技术栈
- PX4添加新的应用
- Learning summary (about deep learning, vision and learning experience)
- 2020-11-05
猜你喜欢
python_scrapy_房天下
Improvement of rate limit for laravel8 update
软件测试就是这么回事?!
YGC troubleshooting, let me rise again!
PCIe 枚举过程
5G+AR出圈,中国移动咪咕成第33届中国电影金鸡奖全程战略合作伙伴
Which is more worth starting with the difference between vivos7e and vivos7
laravel8更新之速率限制改进
Search and replace of sed
Deeplight Technology Bluetooth protocol SRRC certification services
随机推荐
狗狗也能操作无人机!你没看错,不过这其实是架自动驾驶无人机 - 知乎
解析Istio访问控制
Mozi college SQL injection solution
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Learning summary (about deep learning, vision and learning experience)
sed之查找替换
Spotify是如何推动数据驱动决策的?
print( 'Hello,NumPy!' )
The young generation of winner's programming life, the starting point of changing the world is hidden around
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
将“光头”识别为“足球”,AI 摄像头如何犯的错?
That's what software testing is all about?!
shiyou的数值分析作业
成功解决An error ocurred while starting the kernel
墨者学院SQL注入解题
5g/4g工业无线路由器
Japan PSE certification
蓝牙2.4G产品日本MIC认证的测试要求
It's 20% faster than python. Are you excited?
新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务