[LeetCode](week1) 11.Container W

2018-09-09  本文已影响0人  jeff98

题目描述

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.

Note: You may not slant(倾斜) the container and n is at least 2.

题解

class Solution
{
  public:
    int maxArea(vector<int> &height)
    {
        int size = height.size();
        int max = 0;
        for (int i = 0; i < (size - 1); ++i)
        {
            for (int e = i + 1; e < size; ++e)
            {
                int temp = (height[i] > height[e]) ? height[e] : height[i];
                // cout << "\t" << i << "\t" << e << "\t" << temp << "\t" << (e - i) * temp << endl;
                if ((e - i) * temp > max)
                    max = (e - i) * temp;
            }
        }
        return max;
    }
};
上一篇下一篇

猜你喜欢

热点阅读