Esempio n. 1
0
        // 695. Max Area of Island
        public int MaxAreaOfIsland(int[][] grid)
        {
            var m           = grid.Length;
            var n           = grid[0].Length;
            var totalLength = m * n;
            var uf          = new UnionFind(totalLength + 1);
            var maxArea     = 0;

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (grid[i][j] == 0)
                    {
                        uf.Union(i * n + j, totalLength);
                    }
                    else
                    {
                        if (j + 1 < n && grid[i][j + 1] == 1)
                        {
                            uf.Union(i * n + j, i * n + j + 1);
                        }
                        if (i + 1 < m && grid[i + 1][j] == 1)
                        {
                            uf.Union((i + 1) * n + j, i * n + j);
                        }
                        maxArea = Math.Max(maxArea, uf.FindSize(i * n + j));
                    }
                }
            }

            return(maxArea);
        }
        // 128. Longest Consecutive Sequence
        // A small tweak on UnionFind to add the FindSize method for this problem
        public int LongestConsecutive(int[] nums)
        {
            var uf        = new UnionFind(nums.Length);
            var dict      = new Dictionary <int, int>();
            var maxLength = 0;

            for (int i = 0; i < nums.Length; i++)
            {
                if (!dict.ContainsKey(nums[i]))
                {
                    dict.Add(nums[i], i);
                }
                else
                {
                    continue;
                }

                if (dict.ContainsKey(nums[i] - 1))
                {
                    uf.Union(i, dict[nums[i] - 1]);
                }
                if (dict.ContainsKey(nums[i] + 1))
                {
                    uf.Union(i, dict[nums[i] + 1]);
                }

                maxLength = Math.Max(maxLength, uf.FindSize(i));
            }

            return(maxLength);
        }
Esempio n. 3
0
        public int CalculateEnclosedArea(int[][] grid)
        {
            var m          = grid.Length;
            var n          = grid[0].Length;
            var totalCells = m * n;
            var uf         = new UnionFind(totalCells + 3);

            // uf[totalCells] -> 0 cell that is not enclosed,
            // uf[totalCells + 1] -> wall cells(value >= 1),
            // uf[totalCells + 2] -> enclosed 0 cells

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (grid[i][j] > 0)
                    {
                        uf.Union(i * n + j, totalCells + 1);
                    }

                    if (grid[i][j] == 0)
                    {
                        if (i == 0 || j == 0 || i == m - 1 || j == n - 1)
                        {
                            uf.Union(i * n + j, totalCells);
                        }

                        if (i + 1 < m && grid[i + 1][j] == 0)
                        {
                            uf.Union(i * n + j, (i + 1) * n + j);
                        }
                        if (j + 1 < n && grid[i][j + 1] == 0)
                        {
                            uf.Union(i * n + j, i * n + j + 1);
                        }
                    }
                }
            }

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (grid[i][j] == 0 && !uf.Connected(i * n + j, totalCells))
                    {
                        uf.Union(i * n + j, totalCells + 2);
                    }
                }
            }

            return(uf.FindSize(totalCells + 2) - 1);
        }