AcWing 1575. 盛水最多的容器——Java代码版——双指针
原题链接
简单
作者:
三玖天下第一
,
2021-03-26 14:57:36
,
所有人可见
,
阅读 454
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(reader.readLine().trim());
String[] temp = reader.readLine().split(" ");
int[] h = new int[n+1];
for (int i = 0; i < n; i++) {
h[i] = Integer.parseInt(temp[i]);
}
int res = 0;
for(int i = 0, j = n-1; i < j; ) {
res = Math.max(res, Math.min(h[i], h[j]) * (j-i));
if (h[i] < h[j]) i++;
else j--;
}
System.out.println(res);
}
}