LeetCode 11 Container With Most Water (max. Water Container)

Translation

`Given n non-negative integers a1, a2,..., an, each of which represents a coordinate (I, ai ). N vertical line segments, such as the two endpoints of a line segment, are in (I, ai) and (I, 0 ). Locate two line segments and form a container with the X axis to make them contain the most water. Note: you do not have to dump the container.`

Translation

`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.`

The question means that each number in the array corresponds to the length of a line segment, and the index corresponds to the x coordinate. The two indexes can form a width at the bottom, and the height is the length of the Line Segment mentioned above, the height is the shorter of the two line segments.

So how can we solve the problem?

You are not good at the level or in English, so you can use the simplest method at the beginning to try to understand the meaning of the question, even if the time/space limit is exceeded.

`public int MaxAera(int[] height){ int area = 0; for (int i = 0; i < height.Length; i++) { for (int j = i + 1; j < height.Length; j++) { if (height[i] < height[j]) area = Math.Max(area, countArea(height, i, j)); } } return area;}public int countArea(int[] height, int x, int y){ int h = height[x] > height[y] ? height[x] : height[y]; int info = h * (y - x); return info;}`

Obviously, this is not the case ......

What can be simplified?

The previous method is to traverse all the situations one by one from the left of the array, but it is obvious that you can step forward from both sides to the middle and use the corresponding max function to keep the maximum area.

When you enter line 1 on the left and 5 on the right. You don't want to go to line segments 6 and 7 on the right, because you came from there.

So should we go from the left to the right or from the right to the left?

If you go to the left on the right side, although line 1 is changed to line 2, the distance between line 1 and line 5 is larger than line 2, so the area is large. Therefore, the area is small after it is taken.

If it is left on the right,**Complete the brain in person: Line Segment 3 and line segment 4 are in the same position**If it is to line segment 4, the height of the container will change from the length of the original line segment 5 to the length of Line Segment 1. (although the distance decreases, the total area may be smaller, but please continue to look down), and if the line segment 3, although the height is reduced, the width is reduced, but what then? Because your maxArea is still there, after each calculation, the original value will be overwritten only when the height exceeds the original height.

`maxArea = Max(maxArea,newArea);`

That is to say, if the height does not exceed, there will be no impact.

As for whether it will cross the border due to auto-increment and auto-increment, if

`int[] height = {10, 1, 2, 3, 4, 5, 6, 7, 11};`

Assume that 10 and 11 correspond to line segments 1 and 6,**Please make up your mind: Remove line segment 7**Since Line Segment 1 is shorter than line segment 6, it is left ++, not left -. Therefore, it will not cross the border. And vice versa.

`public class Solution{ public int MaxArea(int[] height) { int left = 0, right = height.Length - 1; int maxArea = 0; while (left < right && left >= 0 && right <= height.Length - 1) { maxArea = Math.Max(maxArea, Math.Min(height[left], height[right]) * (right - left)); if (height[left] > height[right]) { right--; } else { left++; } } return maxArea; }}`

Continue tomorrow. Come on!