当前位置:网站首页>Array - 11. Containers with the most water
Array - 11. Containers with the most water
2022-07-23 22:15:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Container for the most water
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 .
2 Title Example

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
3 Topic tips
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
4 Ideas
The area of the matrix is related to two factors :
The length of the matrix : The distance between two vertical lines
The width of the matrix : The length of the shorter of the two vertical lines
therefore , To maximize the area of the matrix , The farther the two vertical lines are, the better , The shortest length of two vertical lines should be as long as possible .
We set two pointers left and right, Point to the leftmost and rightmost ends of the array respectively . here , The distance between the two vertical lines is the farthest , If the area of the next matrix is larger than the current area , We must height[left] and height[right] The shorter vertical line moves towards the middle , See if you can find a longer vertical line .
For this kind of problem , Don't think about the whole , Instead, think about the parts ; The essence is dynamic planning thinking , Consider how to deal with every sub problem, that is : Location i, How much water can it hold .
5 My answer
/** * Double pointer solution * The time complexity is reduced O(N), Spatial complexity O(1) */
public int maxArea(int[] height) {
int size = height.length;
int result = 0;
int left_max = 0, right_max = 0;
int left = 0, right = size - 1;
while (left < right) {
left_max = Math.max(left_max, height[left]);
right_max = Math.max(right_max, height[right]);
int currentArea = 0;
// use left and right The two pointers shrink from both ends to the center , Calculate while shrinking [left, right] The rectangular area between , Taking the largest area value is the answer
// The height of the rectangle is determined by min(height[left], height[right]) That is, determined by the lower side :
// Move the lower side , That side may get higher , Make the height of the rectangle larger , And then 「 There may be 」 Make the area of the rectangle larger .
if (left_max < right_max) {
currentArea = left_max * (right - left);
left++;
} else {
currentArea = right_max * (right - left);
right--;
}
result = Math.max(result, currentArea);
}
return result;
}
边栏推荐
- [hiflow] Tencent cloud's new generation of automation assistant, which I used to complete the enterprise epidemic prompt (no code)
- Storage structure and management disk. It's a bit like installing Win98. You need to partition and format the hard disk first
- University database creation and query practice -- database table design
- Redis常用命令对应到Redisson对象操作
- 产品生命周期,常见的项目职能,信息流 都是什么
- 『晨读』如果你处在我的位置,你会怎么做?我们怎么样做,
- Ali onedate's layered thought
- Sudoku written for once and for all
- 接口测试
- 记第一次挖洞交洞历程
猜你喜欢

【数学建模暑期培训】配送中心选址问题

机器学习习题——对率回归
![[golang learning notes] is parameter transfer in go language value transfer or reference transfer](/img/79/57b28d0e6501132c347f7e7c0516b3.png)
[golang learning notes] is parameter transfer in go language value transfer or reference transfer

El select drop-down box multi selection remote search anti display

U++ 事件

DBSCAN point cloud clustering

Leetcode high frequency question 53. maximum subarray sum, continuous subarray with maximum sum, return its maximum sum

MySQL的JDBC編程

达梦数据库tools包中的工具(操作达梦数据库)

LeetCode高频题62. 不同路径:机器人从左上角到右下角的路径有多少条?纯概率排列组合问题,而不是动态规划题
随机推荐
10道面试基础笔试题,你能对几题?
软件测试工作内容太简单怎么办?
Taobao assistant is disabled. What is the reason for using the big Taoying import data package to upload the baby prompt "the main image is required and cannot be empty"? How to solve it?
lambda学习(sort后面的Comparator的使用,collection后使用Collectors.groupingBy分组)
TreeMap
Jedis 6 - Introduction and difference between redisson and jedis
【数学建模暑期培训】配送中心选址问题
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
海外资深玩家的投资建议(3) 2021-05-04
Wangxuegang video coding -- mediacodec coding and decoding
Microsoft SQL Server database language and function usage (XIII)
Still worrying about xshell cracking, try tabby
Tools in the tools package of Damon database (operate Damon database)
Record the process of the first excavation and intersection
机器学习习题——对率回归
【学习笔记】树的直径,重心
How ZK solves the problem of cerebral fissure
Neo4j application
Can Verilog of synthetizable be integrated
初探POC编写