[Sheng] LeetCode 11 most water containers

Topic Link

【answer】


Double pointer.
Start l = 0, r = len- 1
was then continuously shrinking to the middle.
If you find h [l] <h [r ].
If you let r diminishing, you will find that as long as h [l] is the left point. (Right end (than it is larger or smaller than him)) certainly not better to the current situation.
So the only l ++
h [l]> h [r ] Similarly

Initially thought was a nlogn approach.
A first row in descending order (in height).
Then the order of enumeration i
obviously 1..i there's a board consisting of rectangular, it must be based on the height of the board of the i-th subject (minimum).
So the current task is to find an index in which it is furthest away the board.
And then taking the maximum value for all i.

[Code]

class Solution {
public:
    int maxArea(vector<int>& height) {
         int len = height.size();
         int l = 0,r = len-1;
         int ma = min(height[0],height[r])*r;
         while (l<r){
             if (height[l]<height[r]) l++; 
             else r--;
             ma = max(ma,(r-l)*min(height[l],height[r]));
         }
         return ma;
    }
};

Guess you like

Origin www.cnblogs.com/AWCXV/p/11802319.html