Leetcode 1020. Number of Enclaves - 彻底掌握并查集(Union Find)系列题11

You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.

move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.

Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

Example 1:

Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.

Example 2:

Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation: All 1s are either on the boundary or can reach the boundary.

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 500
  • grid[i][j] is either 0 or 1.

读完题目感觉似曾相识,没错这道题跟Leetcode1254. Number of Closed Islands高度相似。唯一一点区别是,一道题是求封闭岛屿的总个数,另一道是求所有封闭岛屿包含的陆地总个数。解题思路和代码几乎可以完全照搬Leetcode1254. Number of Closed Islands

class UnionFind :
    def __init__(self, n) :
        self.parent = [-1] * n
        self.size = [0] * n
        self.isClosed = [False] * n  #记录所在岛屿是否封闭
        
    def find(self, x) :        
        if self.parent[x] != x :
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]
    
    def merge(self, x, y) :
        px, py = self.find(x), self.find(y)
        #两个同属一个岛屿直接返回,否则进行合并
        if px == py :
            return
        
        #小岛合并到大岛,为了提高查找的效率
        if self.size[px] > self.size[py] :
            self.parent[py] = px
            self.size[px] += self.size[py]
            
            self.isClosed[px] &= self.isClosed[py] 
        else :
            self.parent[px] = py
            self.size[py] += self.size[px]
            self.isClosed[py] &= self.isClosed[px]
        
    #每增加一新陆地需要进行更新parent并岛的个数加1
    def updateParent(self, x, closed) :
        if self.parent[x] == -1 :
            self.parent[x] = x
            self.size[x] = 1
            self.isClosed[x] = closed
            
class Solution:
    def numEnclaves(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        if m < 3 or n < 3 : 
            return 0
        
        uf = UnionFind(m * n)
  
        for i in range(m) :
            for j in range(n) :
                if grid[i][j] == 1 :
                    close = True if (i > 0 and i < m - 1 and j > 0 and j < n - 1 ) else False
                    uf.updateParent(i * n + j, close)
                    if i - 1 >= 0 and grid[i - 1][j] == 1 :
                        uf.merge(i * n + j, (i - 1) * n + j )
                    if j - 1 >= 0 and grid[i][j - 1] == 1 :
                        uf.merge(i * n + j, i * n + j - 1)
        res = 0
        for i in range(m) :
            for j in range(n) :
                index = i * n + j
                if uf.parent[index] == index and uf.isClosed[index] == True:
                    res += uf.size[index]
        
        return res        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值