jayati

Container with most water

Sep 4th, 2022
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. //two-pointer approach considering that the pillar which is more far will hold more water regardless of its height
  2. class Solution {
  3. public:
  4. int maxArea(vector<int>& height) {
  5. vector<pair<int,int>> v;
  6. vector<int> contain;
  7.  
  8. for(int i=0;i<height.size();i++)
  9. {
  10. v.push_back(make_pair(height[i],i+1));
  11. }
  12. int l=0;
  13. int r=v.size()-1;
  14. int max_area=0;
  15. while(l<r)
  16. {
  17.  
  18. max_area=max(max_area,min(v[l].first,v[r].first)*(v[r].second-v[l].second));
  19.  
  20. contain.push_back(max_area);
  21. if(v[l].first>v[r].first)
  22. {
  23. r--;
  24. }
  25. else if(v[l].first<=v[r].first)
  26. {
  27. l++;
  28. }
  29. }
  30.  
  31. int ans=*max_element(contain.begin(),contain.end());
  32. return ans;
  33. }
  34. };
Advertisement
Add Comment
Please, Sign In to add comment