Transmission Door
Container with most watermy submissionsQuestionTotal accepted:72326 Total submissions:211394 difficulty:medium
Given n non-negative integers A1, A2, ..., an, where each represents a point at Coordi Nate (i, ai). N Vertical Lines is drawn such that the and the both endpoints of line I am at (i, ai) and (I, 0). Find lines, which together with X-axis forms a container, such that the container contains the most water.
Note:you may not slant the container.
Subscribe to see which companies asked this question
Hide TagsArray of PointersShow Similar Problems Test instructions and the problem turn from: http://blog.csdn.net/feliciafay/article/details/17215675 very good greedy question, praise
Then guess, it is necessary to use dynamic programming, but found that there is no way to write the state transfer equation. Really can not find a way, but found that the online solution has a very concise O (N) time complexity of the answer, should be said to be considered a greedy strategy.
Two subscript variables representing the head and tail of the array, respectively, moving towards the center. The process of this passage is this:
Assuming now is the initial state, subscript variable i=0 represents the head, subscript variable j=height.size (), indicating the tail, then obviously at this time the container's water volume depends on the size of a rectangle, the length of the rectangle is j-i, the height is height[i] and height[j] the minimum value ( Assuming Height[i] is less than height[j]). Next consider whether to move the head subscript I to the right or the tail subscript J to the left? If the trailing variable j is moved, then even if the height[j] gets higher, the volume of water still does not become larger because the short plate is in the head variable i. So I should move the head variable i. That is, each time you move one of the head variable I and the trailing variable j, which variable has a small height, move the variable to the center. Calculates the amount of water at this time and compares it with the current value of the maximum volume of water to be loaded.
The answers are as follows, with 100ms passed on the OJ of Leetcode.
Summary
(1) Moving from both sides to the middle is a good idea. To think about it also fits the style of the maximum area of the problem.
(2) Each time the subscript is updated, in the end update I or update j? The problem is interesting in that the updated standard is not usually understood I or J which must be better update which, but which can be better on which to update which, or, if the update I will be worse, then update J look will not be good.
Code:
1 classSolution {2 Public:3 intMaxarea (vector<int>&height) {4 intMa =0;5 intTe =0;6 inti,j;7 intle;8i =0; j = height.size ()-1;9Le =J;Ten while(I <j) { One if(Height[i] <=Height[j]) { ATe = le *Height[i]; -Ma =Max (ma,te); -i++; the } - Else{ -Te = le *Height[j]; -Ma =Max (ma,te); +j--; - } +le--; A } at returnMa; - } -};
Leetcode 11. Container with most water