[LeetCode from scratch] Container With Most Water
Question:
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.
Answer:
Brute force solution, needless to say, timeout cannot be AC:
class Solution {public: int area(int i, int j, int ai, int aj) { int length = (i - j > 0) ? (i - j) : (j - i); int width = (ai > aj) ? aj : ai; return (length * width); } int maxArea(vector
& height) { int size = height.size(); int ans = -1; for(int i=1; i <= size; i++) { for(int j=1; j <= size; j++) { int tmp = area(i, j, height[i-1], height[j-1]); if(tmp > ans) ans = tmp; } } return ans; }};
Further consideration,
In O (N ^ 2), do I have to consider and detect each situation?
If there is a container, the height of the baffle on the left side is h_a, and that on the right side is h_ B (h_a> h_ B ). At this time, the baffle plate at the fixed position B does not move,Move the baffle at a TO ~ Any part of the B range is meaningless.. Because:
If the height of the moved baffle plate is higher than h_ B, the height of the container is determined by the lower h_ B. At the same time, the width of the container is reduced. If the height of the moved baffle is lower than h_ B, the height of the container is reduced, and the width of the container is also reduced,
UniqueThe method that may reach a larger area in the interval is
Raise the short board. In order to increase the height of the short board so as to increase the height of the entire container and make up for the narrowing of the width. There is another problem here. If my optimal value is not ~ What should I do within B? In this way, the search within the interval is meaningless. Therefore, we need to limit the initial interval to the entire interval. Then, each optimal search ensures the optimization of the final result, without missing the optimal value because of the moving of the left and right sides.
class Solution {public: int area(int i, int j, int ai, int aj) { int length = (i - j > 0) ? (i - j) : (j - i); int width = (ai > aj) ? aj : ai; return (length * width); } int maxArea(vector
& height) { int size = height.size(); int left = 0, right = size - 1; int ans = area(left, right, height[left], height[right]); while(left < right) { if(height[left] < height[right]) left++; else right--; int tmp = area(left, right, height[left], height[right]); ans = (ans > tmp)? ans: tmp; } return ans; }};
To prove the correctness of the algorithm, you can use the reverse verification method (portal ). You can also think like this: the next larger area changes from the current state. The current status is
Remove impossible situationsGradually
TraversalSo it will not be smaller than the area of the previous state, but will be larger. Therefore, the final result must be the maximum value.