当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Face recognition: attack types and anti spoofing techniques
- C++在C的基础上改进了哪些细节
- Rust:命令行参数与环境变量操作
- How does spotify drive data-driven decision making?
- Iqkeyboardmanager source code to see
- 笔试面试题目:求缺失的最小正整数
- Adobe Lightroom /Lr 2021软件安装包(附安装教程)
- YGC问题排查,又让我涨姿势了!
- Daily challenges of search engines_ 4_ External heterogeneous resources - Zhihu
- [data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
猜你喜欢
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
笔试面试题目:判断单链表是否有环
Face recognition: attack types and anti spoofing techniques
Learning summary (about deep learning, vision and learning experience)
不多不少,大学里必做的五件事(从我的大一说起)
laravel8更新之速率限制改进
AMD Zen3首发评测:频率超5GHz,IPC提升不止19%,这次真的Yes了 - 知乎
推荐一部经济科普视频,很有价值!
Distributed consensus mechanism
Mate 40 series launch with Huawei sports health service to bring healthy digital life
随机推荐
laravel8更新之速率限制改进
ArrayList源码分析
软件测试培训班出来好找工作么
Adobe media encoder /Me 2021软件安装包(附安装教程)
Rust:命令行参数与环境变量操作
Fgagt: flow guided adaptive graph tracking
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
Tiktok live monitoring Api: random recommendation
Spotify是如何推动数据驱动决策的?
Flink's sink: a preliminary study
新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务
糟糕,系统又被攻击了
i5 1135g7和i5 1035g1参数对比区别大吗? 哪个好
PCIe 枚举过程
Second assignment
Flink的sink实战之一:初探
5G+AR出圈,中国移动咪咕成第33届中国电影金鸡奖全程战略合作伙伴
ASP.NET A complete solution based on exception handling in MVC
ASP.NET MVC下基于异常处理的完整解决方案
Game mathematical derivation AC code (high precision and low precision multiplication and division comparison) + 60 code (long long) + 20 point code (Full Permutation + deep search DFS)