Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 679 Bytes

File metadata and controls

25 lines (21 loc) · 679 Bytes

Insigths

if v[i] < v[j], then for any pair (v[i], v[x], i < x < j) the water it can contain is <= the water the current pair can contain.

Discuss

#include <algorithm>

class Solution {
public:
    int maxArea(vector<int>& v) {
        // v.size() is at least 2
        int i = 0, j = v.size() - 1;
        int max = 0;
        while (i < j) {
            int tmp = (j - i) * std::min(v[i], v[j]);
            max = tmp > max ? tmp : max;
            if ( v[i] < v[j] ) i++;
            else j--;
        }
        return max;
    }
};