[leetcode] 11. Container With Most Water (medium)

原题链接

以Y坐标长度作为木桶边界,以X坐标差为桶底,找出可装多少水。

思路:

前后遍历。

Runtime: 5 ms, faster than 95.28% of Java

class Solution {
        public int maxArea(int[] height) {
            int res = 0;
            int beg = 0;
            int end = height.length - 1;
            int temp = 0;
            while (beg != end) {
                temp = Math.min(height[beg], height[end]) * (end - beg);
                res = temp > res ? temp : res;
                if (height[beg] > height[end])
                    end--;
                else
                    beg++;
            }
            return res;
        }

 

posted @ 2018-11-09 08:49  Ruohua3kou  阅读(80)  评论(0编辑  收藏  举报