Example #1
0
            public int TrapRainWater(int[][] heightMap)
            {
                int rows = heightMap.Length;
                int cols = heightMap[0].Length;

                if (cols < 3 || rows < 3)
                {
                    return(0);
                }
                bool[,] visited = new bool[rows, cols];
                DuplicatedSortedTree <int[]> heap = new DuplicatedSortedTree <int[]>(new RainComparer(), new RainComparer());

                for (int i = 0; i < rows; i++)
                {
                    heap.Add(new int[] { i, 0, heightMap[i][0] });
                    heap.Add(new int[] { i, cols - 1, heightMap[i][cols - 1] });
                    visited[i, 0]        = true;
                    visited[i, cols - 1] = true;
                }
                for (int i = 1; i < cols - 1; i++)
                {
                    heap.Add(new int[] { 0, i, heightMap[0][i] });
                    heap.Add(new int[] { rows - 1, i, heightMap[rows - 1][i] });
                    visited[0, i]        = true;
                    visited[rows - 1, i] = true;
                }
                int ans = 0;

                int[] d = new int[] { -1, 0, 1, 0, -1 };
                while (heap.Count > 0)
                {
                    var temp = heap.Min;
                    heap.Remove(temp);
                    for (int i = 0; i < 4; i++)
                    {
                        int r = temp[0] + d[i];
                        int c = temp[1] + d[i + 1];
                        if (r >= 0 && r < rows && c >= 0 && c < cols && !visited[r, c])
                        {
                            if (heightMap[r][c] < temp[2])
                            {
                                ans += temp[2] - heightMap[r][c];
                            }
                            visited[r, c] = true;
                            heap.Add(new int[] { r, c, Math.Max(temp[2], heightMap[r][c]) });
                        }
                    }
                }
                return(ans);
            }
Example #2
0
        public IList <IList <int> > GetSkyline(int[][] buildings)
        {
            List <int[]> buildings2 = new List <int[]>(buildings.Length * 2);

            foreach (var b in buildings)
            {
                buildings2.Add(new int[] { b[0], -b[2] });
                buildings2.Add(new int[] { b[1], b[2] });
            }
            buildings2.Sort((x, y) =>
            {
                var c = x[0].CompareTo(y[0]);
                if (c == 0)
                {
                    c = x[1].CompareTo(y[1]);
                }
                return(c);
            });

            List <IList <int> >        ans  = new List <IList <int> >();
            DuplicatedSortedTree <int> heap = new DuplicatedSortedTree <int>();
            int prev = 0;

            foreach (var b in buildings2)
            {
                if (b[1] < 0)
                {
                    heap.Add(-b[1]);
                }
                else
                {
                    heap.Remove(b[1]);
                }
                int max = 0;
                if (heap.Count > 0)
                {
                    max = heap.Max;
                }
                if (max != prev)
                {
                    ans.Add(new List <int> {
                        b[0], max
                    });
                }
                prev = max;
            }
            return(ans);
        }