当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Japan PSE certification
- “智能5G”引领世界,数位智能网优+5G能带来什么?
- 双向LSTM在时间序列异常值检测的应用
- Is software testing training class easy to find a job
- Search and replace of sed
- How TCP protocol ensures reliable transmission
- Tiktok live monitoring Api: random recommendation
- Personal current technology stack
- Python learning Day1 -- Basic Learning
- 数据科学面试应关注的6个要点
猜你喜欢
Improvement of rate limit for laravel8 update
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
python_scrapy_房天下
临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴
Shiyou's numerical analysis assignment
Mate 40 series launch with Huawei sports health service to bring healthy digital life
ulab 1.0.0发布
解决RabbitMQ消息丢失与重复消费问题
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
Flink的sink实战之一:初探
随机推荐
vivoy73s和荣耀30青春版的区别
Is software testing training class easy to find a job
Adobe Prelude / PL 2020 software installation package (with installation tutorial)
PCIe enumeration process
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
Flink的sink实战之一:初探
个人目前技术栈
面部识别:攻击类型和反欺骗技术
VC++指定目录下文件按时间排序输出
Julia 是如何风靡起来的?
We interviewed the product manager of SQL server of Alibaba cloud database, and he said that it is enough to understand these four problems
Second assignment
Rust: command line parameter and environment variable operation
laravel8更新之速率限制改进
比Python快20%,就问你兴不兴奋?
Mozi college SQL injection solution
Python Gadgets: code conversion
2天,利用下班后的4小时开发一个测试工具
运维人员常用到的 11 款服务器监控工具
不多不少,大学里必做的五件事(从我的大一说起)