当前位置:网站首页>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;
}
}
边栏推荐
- 302. minimum rectangular BFS with all black pixels
- Self attention and multi head self attention (MSA) in transformer
- Market trend report, technical innovation and market forecast of microencapsulated chemical pesticides in China
- Installing rainbow in various kubernetes with Helm
- Data visualization practice: Experimental Report
- Transaction and message semantics
- MySQL-09
- Install pyinstaller
- 技术能力的思考和总结
- China micro cultivator market trend report, technical dynamic innovation and market forecast
猜你喜欢

Getting started with Python

MySQL-08

MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

How to select and build a real-time data warehouse scheme

EFK升级到ClickHouse的日志存储实战

Use the fast proxy to build your own proxy pool (mom doesn't have to worry about IP being blocked anymore)
The sysdig 2022 cloud native security and usage report found that more than 75% of the running containers have serious vulnerabilities
![[intra group questions semester summary] some reference questions for beginners](/img/39/ba5b7ce3ab86433f29c9fa3ced4ddd.jpg)
[intra group questions semester summary] some reference questions for beginners

连接数服务器数据库报:错误号码2003Can‘t connect to MySQL server on ‘服务器地址‘(10061)

GoF23—建造者模式
随机推荐
Mongodb -- use mongodb to intercept the string content in the field and perform grouping statistics
Redis multithreading and ACL
Tencent WXG internship experience (has offered), I hope it will help you!
MySQL-07
Implement the runnable interface
canal部署、原理和使用介绍
Message queuing - omnidirectional comparison
Selective search for object recognition paper notes [image object segmentation]
The sysdig 2022 cloud native security and usage report found that more than 75% of the running containers have serious vulnerabilities
Volatile application scenarios
Data visualization practice: Data Visualization
Alarm operation and Maintenance Center | build an efficient and accurate alarm collaborative processing system
Install pyinstaller
Logstash——使用throttle过滤器向钉钉发送预警消息
Gof23 - abstract factory pattern
DS18B20详解
Installing rainbow in various kubernetes with Helm
How to design a good technical scheme
Logstash - logstash sends an alarm email to email
Redis underlying data structure