CODE 120: Container With Most Water

来源:互联网 发布:java中的sleep 编辑:程序博客网 时间:2024/05/17 06:48

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

public int maxArea(int[] height) {// Start typing your Java solution below// DO NOT write main() functionif (null == height || height.length <= 0) {return 0;}int max = Integer.MIN_VALUE;for (int i = 0; i < height.length; i++) {for (int j = 0; j < i; j++) {if (height[j] >= height[i]) {int tmp = (i - j) * height[i];if (tmp > max) {max = tmp;}break;}}for (int j = height.length - 1; j > i; j--) {if (height[j] >= height[i]) {int tmp = (j - i) * height[i];if (tmp > max) {max = tmp;}break;}}}return max;}


原创粉丝点击