当前位置:网站首页>[leetcode] 11. Container with the most water - go language solution
[leetcode] 11. Container with the most water - go language solution
2022-07-24 07:15:00 【Cabbage that wants to become powerful】
List of articles
One 、 Title Description
Given a length of n Array of integers for height . Yes n Vertical line , The first i The two ends of the line are (i, 0) and (i, height[i]) .
Find two of them , Make them x A container of shafts can hold the most water .
Return the maximum amount of water that the container can store .
explain : You can't tilt the container .
Example 1:
Input :[1,8,6,2,5,4,8,3,7]
Output :49
explain : The vertical line in the figure represents the input array [1,8,6,2,5,4,8,3,7]. In this case , The container can hold water ( In blue ) The maximum value of is 49.
Example 2:
Input :height = [1,1]
Output :1
Tips :
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
Topic link :https://leetcode.cn/problems/container-with-most-water/
Two 、 Their thinking - Double finger needling
Observe intuitively :
- The bottom of the container must be as wide as possible , This can hold more water .
- The higher the two walls of the container, the better .
- The amount of water in the container depends on the shorter wall .
Start from the first intuition , We can use From wide to narrow How to search for : Use two pointers to represent the left and right boundaries of the container , Shilling points to both ends of the array , Then gradually move to the middle to search , Until the two hands meet .
Start from the second intuition , We know that the higher the two walls, the better , Then the way to move the pointer is : Which pointer to move ? Which wall is shorter at present , We move the pointer to the wall forward .
Every time you move , Calculate the maximum water volume of the current container , Pay attention to : The effective height of the container shall be subject to the shorter wall .
3、 ... and 、 My explanation
Go The language code :
func maxArea(height []int) int {
i:=0
j:=len(height)-1
max := 0
for i != j {
if height[i]<=height[j]{
if (j-i)*height[i] > max {
max = (j-i)*height[i]
}
i++
}else{
if (j-i)*height[j] > max {
max = (j-i)*height[j]
}
j--
}
}
return max
}
Judge the result :
边栏推荐
- Gimp custom screenshot
- ROS starts non native nodes
- [PTA] group programming ladder competition - Summary of exercises L3 (incomplete)
- 【LeetCode】11. 盛最多水的容器 - Go 语言题解
- 【C语言入门】ZZULIOJ 1011-1015
- Vs2019 configuration running open3d example
- Tensorflow Einstein function
- 树莓派换源
- 上传excel文件
- Chapter007-FPGA学习之IIC总线EEPROM读取
猜你喜欢

项目上线就炸,这谁受得了

cookie_session

解压主播狂揽4000w+播放,快手美食赛道又添新风向?

论文阅读:HarDNet: A Low Memory Traffic Network

Hackingtool of security tools

C language from introduction to soil -- super detailed summary of operators

QoS服务质量三DiffServ模型报文的标记及PHB

Variables and data types (04) end

STM32H750VBT6驱动程控增益放大模块PGA113——基于CubeMX的Hal库

变量和数据类型(03)
随机推荐
ROS starts non native nodes
Redis 哨兵机制
chm文件打开时提示乱码
开发系统选择路线
Accumulation of project problems
解压主播狂揽4000w+播放,快手美食赛道又添新风向?
Aggregated new ecological model - sharing purchase, membership and reward system
File “manage.py“, line 14 ) from exc ^ SyntaxError: invalid syntax出错的原因及解决方法
我的创作纪念日
xavier_ normal_ Initialization test
Wix path with spaces
Wild pointer, null pointer, invalid pointer
Cmake notes
STM32 external interrupt (register version)
Redis 持久化
Use of redis
yocs_ velocity_ Smooth source code compilation
【行测】图形找规律类题目
[tips] a simple method to create a version control project
Paper reading: hardnet: a low memory traffic network