当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 2020-11-05
- Improvement of rate limit for laravel8 update
- Fgagt: flow guided adaptive graph tracking
- [computer network] learning notes, Part 3: data link layer (Xie Xiren version)
- 211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]
- Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
- Architect (November 2020)
- Python Gadgets: code conversion
- 墨者学院SQL注入解题
- 【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)
猜你喜欢
Six key points of data science interview
print( 'Hello,NumPy!' )
Flink的sink实战之一:初探
计算机网络基本概念(五)局域网基本原理
The young generation of winner's programming life, the starting point of changing the world is hidden around
413【毕设课设】基于51单片机无线zigbee无线智能家居光照温湿度传输监测系统
函数周期表丨筛选丨值丨SELECTEDVALUE - 知乎
Spotify是如何推动数据驱动决策的?
TCP协议如何确保可靠传输
YGC问题排查,又让我涨姿势了!
随机推荐
NOIP 2012 提高组 复赛 第一天 第二题 国王游戏 game 数学推导 AC代码(高精度 低精度 乘 除 比较)+60代码(long long)+20分代码(全排列+深搜dfs)
Oops, the system is under attack again
Recommend an economic science video, very valuable!
Function periodic table filter value selectedvalue
Cloud Alibabab笔记问世,全网详解仅此一份手慢无
How does spotify drive data-driven decision making?
Python3.9的7个特性
5G+AR出圈,中国移动咪咕成第33届中国电影金鸡奖全程战略合作伙伴
YGC troubleshooting, let me rise again!
第二次作业
仅用六种字符来完成Hello World,你能做到吗?
[summary series] technical system of Internet server: high performance database index
YGC问题排查,又让我涨姿势了!
What is the difference between vivoy73s and vivoy70s
We interviewed the product manager of SQL server of Alibaba cloud database, and he said that it is enough to understand these four problems
攻防世界之web新手题
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
If you don't understand the gap with others, you will never become an architect! What's the difference between a monthly salary of 15K and a monthly salary of 65K?
Insight -- the application of sanet in arbitrary style transfer
How did Julia become popular?