首页>itarticle>leetcode: 11. container with most water
leetcode: 11. container with most water
admin11月 12, 20200
11. Container With Most Water
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
publicclassSolution{ publicintmaxArea(int[] height){ int left=0; int right = height.length-1; int max=0,area; while(left<right) { int l = height[left]; int r = height[right]; if( l > r){ area = (right-left) * r; while (height[--right] <= r); }else{ area = (right-left) * l; while (height[++left] < l); } if (area > max) max = area; } return max; } }
近期评论