Problem:
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.
题目的意思是,在(x,y)坐标中,每个点做与x轴垂直的直线后,求哪两根直线和x轴所能装的水最多,不能倾斜的意思就是不是指梯形的面积,而是短板效应的矩形面积。先暴力试了下,练了下手感。不出所料N方的超时。
class Solution { public: int maxArea(vector<int> &height) { int area = 0, temparea; int *temp = new int[height.size()]; for (int i = 0; i < height.size(); i++) { int maxN = 0; for (int j = 0; j < height.size(); j++) { if (i != j) { temparea = (height[i]<height[j]?height[i]:height[j]) * abs(j - i); if (temparea > maxN) maxN = temparea; } } temp[i] = maxN; } for (int i = 0; i < height.size(); i++) { if (temp[i] > area) area = temp[i]; } delete[] temp; return area; } };
后来还想,能不能排序之后再判断,发现sort又是不稳定的所以就放弃了。后来发现可以从两边往里收缩的办法解决。两边往里的还有第一题Two Sum也是这样做的。
为什么用两边往里呢,因为我们我们要的面积是两条直线的距离*两条直线短的那条的值,所以,我们先定一个,距离最大就是头和尾了,如果比这个距离小的,又还想比我现在大的话,那就只有高增加才有可能,这个时候就是把短的那条对应的位置往前看一位,看看可不可能有比短的长,且乘出来的面积是比之前大的,如果大,那就记录下来。为什么要用短的那边往里看呢,因为如果长的往里的话,就是下去一根再长也是根据短板效应看短的。根据这个思路自己整理了下代码如下:
class Solution { public: int maxArea(vector<int> &height) { int left = 0, right = height.size() - 1; int maxA = 0; while(left < right) { if (height[left] < height[right]) { int tmp = (right - left) * height[left]; left++; if (tmp > maxA) maxA = tmp; } else { int tmp = (right - left) * height[right]; if (tmp > maxA) maxA = tmp; right--; } } return maxA; } };
这样就Accept了
2015/03/29:
class Solution { public: int maxArea(vector<int> &height) { int l = 0, r = height.size()-1, water = 0; while(l < r){ water = max(water, (r - l) * (height[l] > height[r] ? height[r--] : height[l++])); } return water; } };
python:
class Solution: # @return an integer def maxArea(self, height): water, l, r = 0, 0, len(height)-1 while l < r: water = max(water, (r - l)*min(height[l], height[r])) if height[l] < height[r]: l += 1 else: r -= 1 return water