当前位置:网站首页>Container with the most water
Container with the most water
2022-06-26 06:20:00 【Oh no, less hair】
Here you are. n Nonnegative integers a1,a2,…,an, Each number represents a point in the coordinate (i, ai) . Draw in coordinates n Bar vertical line , Vertical line i The two endpoints of are (i, ai) and (i, 0) . Find two of them , Make them x A container of shafts can hold the most water .
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
Example 3:
Input :height = [4,3,2,1,4]
Output :16
Example 4:
Input :height = [1,2,1]
Output :2
Method 1 ( I failed to pass this method because I exceeded the time limit during the test )
public class Solution {
public int maxArea(int[] height) {
int max = 0;
for (int i = 0; i < height.length - 1; ++i) {
for (int j = i + 1; j < height.length; ++j) {
int area = (j - i)*Math.min(height[i],height[j]);
max = Math.max(area,max);
}
}
return max;
}
}
Method 2 ( Be careful i++ It's assignment before operation )
public class Solution {
public int maxArea(int[] a) {
int max = 0;
for (int i = 0,j = a.length - 1; i < j;) {
int minHeight = a[i] < a[j] ? a[i++] : a[j--];
int area = (j - i+1)*minHeight;// because i++ and i-- They are all assignment first and then operation , So we need to add one more 1 To calculate accurately
max = Math.max(max,area);
}
return max;
}
}
Method 3
class Solution {
public int maxArea(int[] a) {
int i = 0,j= a.length - 1,res = 0;
while (i < j) {
res = a[i] < a[j] ?
Math.max(res,(j - i)*a[i++])://i++ and i-- They are all assignment first and then operation , At this time or i
Math.max(res,(j - i)*a[i--]);
}
return res;
}
}
边栏推荐
- Market trend report, technical innovation and market forecast of microencapsulated chemical pesticides in China
- Tencent WXG internship experience (has offered), I hope it will help you!
- 工作积累——Web请求中使用ThreadLocal遇见的问题
- Dpdk - tcp/udp protocol stack server implementation (II)
- PyTorch混合精度原理及如何开启该方法
- Print bit information of numbers
- Logstash - logstash sends an alarm email to email
- typescript的type
- 【Spark】Spark SQL 字段血缘如何实现
- Multi thread synchronous downloading of network pictures
猜你喜欢
随机推荐
Typora activation method
302. minimum rectangular BFS with all black pixels
数据可视化实战:实验报告
Understanding of nil in go language
typescript的type
Import / export function implementation
Architecture design method
Mysql-10 (key)
China micronutrient market trend report, technical innovation and market forecast
Handwritten background management framework template (I)
Efk upgrade to Clickhouse log storage practice
Hot! 11 popular open source Devops tools in 2021!
Redis underlying data structure
Volatile application scenarios
Print bit information of numbers
[spark] how to implement spark SQL field blood relationship
Evolution history of qunar Bi platform construction
实时数仓方案如何选型和构建
[intra group questions semester summary] some reference questions for beginners
打印数字的位信息








