Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //two-pointer approach considering that the pillar which is more far will hold more water regardless of its height
- class Solution {
- public:
- int maxArea(vector<int>& height) {
- vector<pair<int,int>> v;
- vector<int> contain;
- for(int i=0;i<height.size();i++)
- {
- v.push_back(make_pair(height[i],i+1));
- }
- int l=0;
- int r=v.size()-1;
- int max_area=0;
- while(l<r)
- {
- max_area=max(max_area,min(v[l].first,v[r].first)*(v[r].second-v[l].second));
- contain.push_back(max_area);
- if(v[l].first>v[r].first)
- {
- r--;
- }
- else if(v[l].first<=v[r].first)
- {
- l++;
- }
- }
- int ans=*max_element(contain.begin(),contain.end());
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment