I, entitled 1
Second, the idea
Traverse, the maximum output.
Individual optimization ideas: traversal time away from both ends toward the middle.
Third, the code
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;
}
}
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/container-with-most-water
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. ↩︎