题目:

初始化分别位于高度数组开始和结束处的左右两个指针。计算指针当前位置的两行之间的面积。如果当前面积较大,更新最大面积。将高度较小的指针移向另一个指针。
- public class no_11 {
- public static void main(String[] args) {
- int[] arr = {1, 8, 6, 2, 5, 4, 8, 3, 7};
- System.out.println(maxArea(arr));
- }
-
- public static int maxArea(int[] height) {
- int left = 0;
- int right = height.length - 1;
- int maxArena = 0;
- while (left < right) {
- int h = Math.min(height[left], height[right]);
- int w = right - left;
- int arena = h * w;
- maxArena = Math.max(arena, maxArena);
-
- if (height[left] < height[right]) {
- left++;
- } else {
- right--;
- }
- }
- return maxArena;
-
- }
- }