package com.daily.daily20210204;
/**
* @Project: dailyCode
* @Site: http://www.zhao1iang.club/
* @Copyright: ©CodeLamp
* @Author: zhaoliang
* @Create: 2021-02-04 14:21
* @Desc: 盛最多水的容器
**/
public class maxArea {
//给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) 。
// 在坐标内画 n 条垂直线,垂直线 i的两个端点分别为(i,ai) 和 (i, 0) 。找出其中的两条线,使得它们与x轴共同构成的容器可以容纳最多的水。
//
//说明:你不能倾斜容器。
public int maxArea(int[] height){
if(height==null || height.length<2)return 0;
int max =0;
int left=0;
int right = height.length-1;
while(left < right){
max = Math.max(max,(right - left) * Math.min(height[left],height[right]));
if (height[left] < height[right]){
left++;
}else {
right--;
}
}
return max;
}
}
日常刷题-盛最多水的容器
最新推荐文章于 2024-08-23 01:05:03 发布