Test instructions: In the axis of the x-axis of the 0,1,2,3,4 、、、、 n There are n+1 blocks, length, any two plus x-axis can constitute a container, its water area of two board spacing and shorter board length of the product, to the vector container gives a series of values, respectively, represented in the 0,1,2,3,4 、、、 Vector.size ()-1 total size of the short plate, is continuous, does not exclude the board length is 0 of the possibility, but at least two plates. According to the length of the plate, the maximum area of any two plates and x-axis can be obtained and returned.
Code:
1 classSolution {2 Public:3 intMaxarea (vector<int> &height) {4 intMaxarea =0, area;5 intleft =0, right = Height.size ()-1;6 while(Left <Right ) {7Area = (right-left) * (Height[left] < Height[right]?Height[left]: height[right]);8Maxarea = (Maxarea > area)?Maxarea:area;9 if(Height[left] <Height[right])Ten++Left ; One Else A--Right ; - } - returnMaxarea; the } -};
Container with most water
Ideas:
I, Jtraversing from the beginning of the head, respectively, the areaArea = min (Height[j], height[i]) * (j-i),WhenHeight[i] < Height[j], the area at this timeArea = height[i] * (j-i);becauseIIt's a short board, no matter whatTo the right of which plate combination, it can reach the maximum area depends onJ-I.(that is, the distance between the two boards), and at this time the J-iof theValue is the largest, so this area is theIto the left border, with Jto the right borderThe current maximum area, and then++i(Move the short border toward the middle, looking for a higher and larger plate, update area, Continue++i,Until IPlate ratioJboard height before you start moving the right borderJBoard);Jthe changes. Because forI, J, there is always a short board(Equal to a random one), each time the short plate changes, so covering all cases.
Starting analysis from the area = height[i] * (j-i) is easier to understand, and the current, to make it bigger, you have to changeIorJvalues (two arguments in the equation), changing theJthe value, (j-i) will only be smaller, andHeight[i]not changed, then Areait becomes smaller. If it changesIthe value, (j-i) will be smaller, butHeight[i]It 's going to change, justHeight[i]Larger than the current maximum area, it is possible to make Areabecome larger. Now the problem turns into looking for a piece more thanIlonger plates to try to enlarge Area, then fromi+1start by traversing to the right, first looking for a bigger boardB, Judgeheight[b]* (j-b)is greater than Area, IF,i=b, if no, continue the traversal. Each time you look for a higher plate, you need to determine if you need to traverse the direction, because the assumption from the outset isIfor short plates, ifJis a short board, you need toJwalk to the left, Daniel.I(the same is used to analyze the equation).
Each board is traversed at most, so the algorithm complexity O (n), the other algorithm is not necessarily feasible, will time out. If you do not consider the time-out, can be poor to lift any two boards to form the maximum area, of course, can also be pruning, such as: when the first plate to the left with all the board to match, want to find a larger area, must board spacing greater than the current area/height[i], you can cut off the distance I board for area/height[ I] The calculation of some boards below.
Leetcode OJ Container with the most water container for maximum water loading