leetcode Trapping Rain Water

class Solution {
public:
    int trap(int A[], int n) {
        if(n==0)return 0;
        vector<int>left(n);
        int maxHeight = 0;
        for(int i = 0; i < n; i++)
        {
            left[i] = maxHeight;
            maxHeight = max(maxHeight, A[i]);
        }      
        vector<int> right(n);
        maxHeight = 0;
        for(int i = n - 1; i >= 0; i--)
        {
            right[i] = maxHeight;
            maxHeight = max(maxHeight, A[i]);
        }        
        int water = 0;
        for(int i = 0; i < n; i++)
        {
            int height = min(left[i], right[i]) - A[i];
            if (height < 0)
                height = 0;
            water += height;
        }        
        return water;
    }
};

 

posted @ 2013-09-05 15:39  代码改变未来  阅读(246)  评论(0编辑  收藏  举报