Just a c# solution.
There is no builtin PriorityQueue in c#, for those problems that need to use PriorityQueue. There is less discussion.\nI just want to share my own implement abo
There is no builtin PriorityQueue in c#, for those problems that need to use PriorityQueue. There is less discussion.\nI just want to share my own implement about this question with a priority queue implemented by myself.\nbtw, i didn't implement it in a heap way. just leverage the List.BinarySearch for less code.\n\n\n\n```\npublic class Solution {\n    public int[,] directions = new int[,] { {1,0 }, {-1,0 }, {0,1 }, {0,-1 } };\n    public int TrapRainWater(int[,] heightMap)\n    {\n        var total = 0;\n        var m = heightMap.GetLength(0);\n        var n = heightMap.GetLength(1);\n        var visitied = new bool[m, n];\n        var q = new BinaryHeap<int[]>((a, b) => a[2] - b[2]);\n        for (var i = 0; i < m; ++i)\n        {\n            q.Enqueue(new int[] { i, 0, heightMap[i, 0] });\n            q.Enqueue(new int[] { i, n-1, heightMap[i, n-1] });\n            visitied[i, 0] = visitied[i, n - 1] = true;// don't forget to set visited when init the queue\n        }\n\n        for(var i=0;i< n; ++i)\n        {\n            q.Enqueue(new int[] { 0, i, heightMap[0, i] });\n            q.Enqueue(new int[] { m-1, i, heightMap[m-1, i] });\n            visitied[0, i] = visitied[m - 1, i] = true;\n        }\n\n        while (q.Count > 0)\n        {\n            var cell = q.Dequeue();\n            for(var i = 0; i < 4; ++i)\n            {\n                var x = cell[0] + directions[i, 0];\n                var y = cell[1] + directions[i, 1];\n                if (x < 0 || x == m || y < 0 || y == n || visitied[x, y]) continue;\n                total += Math.Max(0, cell[2] - heightMap[x, y]);\n                q.Enqueue(new int[] { x, y, Math.Max(cell[2], heightMap[x,y]) });\n                visitied[x, y] = true;\n            }\n        }\n       \n        return total;\n    }\n        \n    public class BinaryHeap<T>\n    {\n        List<T> list = new List<T>();\n        private Comparison<T> comparison;\n        public BinaryHeap(Comparison<T> comparison)\n        {\n            this.comparison = comparison;\n        }\n\n        public int Count\n        {\n            get\n            {\n                return list.Count;\n            }\n        }\n\n        public T Dequeue()\n        {\n            var item = list[0];\n            list.RemoveAt(0);\n            return item;\n        }\n\n        public void Enqueue(T item)\n        {\n            if (list.Count == 0)\n            {\n                list.Add(item);\n            }\n            else\n            {\n                var i = list.BinarySearch(item, Comparer<T>.Create(comparison));\n                i = i >= 0 ? i : ~i;\n                if(list.Count == i)\n                {\n                    list.Add(item);\n                }\n                else\n                {\n                    list.Insert(i, item);\n                }\n            }\n        }\n\n        public T Seek()\n        {\n            return list[0];\n        }\n    }\n}\n```
[]