Priority_Queue with comments || C++ || Clean Code
\nclass Solution {\npublic:\n    class node\n    {\n        public:\n        int height;\n        int x,y;\n        \n        \n    };\n    class Compare\n    {
```\nclass Solution {\npublic:\n    class node\n    {\n        public:\n        int height;\n        int x,y;\n        \n        \n    };\n    class Compare\n    {\n        public:\n        bool operator() (node const &a,node const &b) {\n            return a.height > b.height;\n        }\n        \n    };\n    int trapRainWater(vector<vector<int>>& heightMap) {\n        int n = heightMap.size();\n        int m = heightMap[0].size();\n        vector<vector<bool>>visited(n,vector<bool>(m,false));\n        priority_queue<node,vector<node>,Compare>pq;\n        \n        // first put the boundary elements into my priority_queue\n        \n        for(int i=0;i<n;i++){\n            for(int j=0;j<m;j++) {\n                \n                    if(!(i==0||j==0||i==n-1||j==m-1)) continue;\n                \n                \n                    visited[i][j] = true;\n                    node t;\n                    t.x = i;\n                    t.y = j;\n                    t.height = heightMap[i][j];\n                    pq.push(t);\n                \n                    \n            }\n        }\n        \n        int ans = 0;\n        int max_ = INT_MIN;\n        \n        vector<vector<int>>dir ={{-1,0},{0,1},{1,0},{0,-1}}; // direction array\n        \n        \n        while(!pq.empty())\n        {\n            node front = pq.top();   // pop the top element which is sorted based on height.\n          \n            pq.pop();\n           \n            max_ = max(max_ , front.height);  // find max height till now\n            int curr_x = front.x;\n            int curr_y = front.y;\n            \n            for(int i=0;i<4;i++)\n            {\n                int new_x = curr_x + dir[i][0];  // new coordinates to explore neighbours\n                int new_y = curr_y + dir[i][1];\n               \n                if(new_x<0||new_y<0||new_x>=n||new_y>=m||visited[new_x][new_y])\n                {\n                    // out of bound cases  or already visited coordinates\n                    continue;\n                }\n              \n            \n               \n                int height = heightMap[new_x][new_y];\n               // if the height of current element is smaller,the add the difference between max and height.\n                if(height<max_)\n                {\n                    ans = ans + (max_ - height);\n                }\n                \n                node temp;\n                temp.x = new_x;\n                temp.y = new_y;\n                temp.height = height;\n                pq.push(temp);\n                visited[new_x][new_y]=true; // mark it visisted\n            }\n            \n            \n            \n            \n        }\n        return ans;\n        \n    }\n};\n```
[]