一、题目1
二、思路
遍历,将最大值输出。
个人优化思路:遍历的时候从两端向中间走。
三、代码
public class T011 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] height = {1,8,6,2,5,4,8,3,7};
System.out.println( maxArea(height) ); //49
}
public static int maxArea(int[] height) {
int area = 0;
for ( int i = 0; i < height.length; i++ ){
for ( int j = i+1; j < height.length; j++ ){
int tmp = (j-i)*Math.min( height[i], height[j] );
if ( tmp > area )
area = tmp;
}
}
return area;
}
}
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/container-with-most-water
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 ↩︎