Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int trap(vector<int>& height) {
- int left = 0, right = height.size() - 1, ans = 0;
- while(left < right) {
- int minH = min(height[left], height[right]);
- //> start from minH to find how much water can be trapped
- if(height[left] == minH) {
- ++left;
- while(left < right && height[left] < minH) {
- ans += minH - height[left];
- ++left;
- }
- }
- else {
- --right;
- while(left < right && height[right] < minH) {
- ans += minH - height[right];
- --right;
- }
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement