Trapping Rain Water1,2 优先队列解法

class Solution {
public:
    int trap(vector& height) {
        pair que;
        int len = height.size();
        if(len<3) return 0;
        int res = 0;
        que.first = 0;
        que.second = len-1;
        int Max = -1;
        while(que.firstMax) Max = height[que.first];
                if(Max>height[que.first+1]) res+=Max - height[que.first+1];
                que.first++;
            }
            else
            {
                if(Maxheight[que.second-1]) res+= Max-height[que.second-1];
                que.second--;
            }
            
        }
        return res;
    }
};

class Solution {
public:
    int trapRainWater(vector>& heightMap) {
        
        int res = 0;
        
        int m = heightMap.size();
        if(m==0) return 0;
        int n = heightMap[0].size();
        priority_queue < pair>,vector>>,greater< pair> > > que;
        
        vector > visited(m,vector(n,0));
        
        int dir[] = {0,1,0,-1,0};
        
        for(int i=0;i> temp = que.top();que.pop();
            if(temp.first>MAX) MAX = temp.first;
            
            int x = temp.second.first;
            int y = temp.second.second;
            
            
            
            for(int i=0;i<4;i++)
            {
                int a = x+dir[i];
                int b = y+dir[i+1];
                
                if(a>=0&&a=0&&b heightMap[a][b])  res += MAX - heightMap[a][b];
                    visited[a][b] = 1;
                }
            }
        }
        return res;
    }
};

你可能感兴趣的:(Trapping Rain Water1,2 优先队列解法)