美文网首页
011. Container With Most Water

011. Container With Most Water

作者: 海湾码农 | 来源:发表于2016-03-13 18:53 被阅读0次

    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.

    找出给定序列中min(nums[i], nums[j])*(j-i)的最大值。

    public class Solution {
      public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int cap = 0;
        
        while (left < right) {
            int tmp = Math.min(height[left], height[right]) * (right - left);
            if (tmp > cap) cap = tmp;
            if (height[left] > height[right]) {
                right--;
            } else {
                left++;
            }
        }
        return cap;
      }
    }

    相关文章

      网友评论

          本文标题:011. Container With Most Water

          本文链接:https://www.haomeiwen.com/subject/tcqflttx.html