class Solution { private: int calculateArea(int left, int right, vector<int>& height){ return (right - left) * min(height[left], height[right]); } public: int trap(vector<int>& height) { int totalArea = 0; if(height.size() <= 2) return 0; int left = 0, right = 0; while(height[left] == 0){ ++left; if(left == height.size()-2) break; } right = left+2; while(left < right && right <= height.size()-1){ totalArea += calculateArea(left, right, height); left = right; right = left+2; } return totalArea; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter