Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 827 · Union Find / DSU

Making A Large Island

LeetCode 827 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

DSU sizes + try flipping each 0.

கடினம்ArrayDepth-First SearchBreadth-First SearchUnion-FindMatrix
LeetCode இல் திற ↗

நான் Making A Large Island (LeetCode 827) ஐ இப்படித் தீர்த்தேன் — Union Find / DSU பேட்டர்னின் ஒரு கடினம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Label every island with union-find (tracking sizes). Then for each water cell, look at the distinct islands around it: flipping it joins them, giving 1 + their sizes. Also consider the grid being all land.

Approach

  1. 1

    Union adjacent land cells (index row·n + col).

  2. 2

    For each 0, collect distinct neighbour roots and sum their sizes + 1.

  3. 3

    Also take the largest existing island.

  4. 4

    Return the maximum.

நான் எப்படித் தீர்த்தேன்

DSU with sizesPythonO(n² α) time
class DisjointSet:
    def __init__(self,n):
        self.size = [1]*(n+1)
        self.parents = [i for i in range(n+1)]
    def find(self,u):
        if self.parents[u]==u:
            return u
        self.parents[u]=self.find(self.parents[u])
        return self.parents[u]
    def union(self,u,v):
        pu,pv = self.find(u),self.find(v)
        if pu==pv:
            return
        if self.size[pu]<self.size[pv]:
            self.parents[pu]=pv
            self.size[pv]=self.size[pv]+self.size[pu]
        elif self.size[pv]<self.size[pu]:
            self.parents[pv]=pu
            self.size[pu]=self.size[pu]+self.size[pv]
        elif self.size[pv]==self.size[pu]:
            self.parents[pv]=pu 
            self.size[pu]=self.size[pu]+self.size[pv]
class Solution:
    def largestIsland(self, grid: List[List[int]]) -> int:
        #first build the dsu based on the connected components - all 1s
        #check for each zero if it has a neighbor with 1s, get the ulimate parent, if two neigbouring parent can be added by changing the current zero, then we can add the neighboring parent's size
        #check for each zero,get max size
        n = len(grid)
        di = [0,+1,0,-1]
        dj = [+1,0,-1,0]
        ds = DisjointSet(n*n)
        for row in range(n):
            for col in range(n):
                if grid[row][col]==1:
                    cur_cell = row*n+col
                    for k in range(4):
                        nrow = row+di[k]
                        ncol = col+dj[k]
                        if 0<=nrow<n and 0<=ncol<n and grid[nrow][ncol]==1:
                            adj_cell = nrow*n+ncol
                            if ds.find(cur_cell)!=ds.find(adj_cell):
                                ds.union(cur_cell,adj_cell)
        max_size=float("-inf")
        for row in range(n):
            for col in range(n):
                if grid[row][col]==1:
                    continue
                size=1
                neighbour_parents=set()
                for k in range(4):
                        nrow = row+di[k]
                        ncol = col+dj[k]
                        if 0<=nrow<n and 0<=ncol<n and grid[nrow][ncol]==1:
                            adj_cell = nrow*n+ncol
                            adj_cell_ulp = ds.find(adj_cell)
                            neighbour_parents.add(adj_cell_ulp)
                for neighbour_parent in neighbour_parents:
                    size+=ds.size[neighbour_parent]
                max_size=max(max_size,size)
        #all 1 case
        for node in range(n*n):
            if ds.parents[node]==node:
                max_size=max(max_size,ds.size[node])
        return max_size
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Making A Large Island — அடிக்கடி கேட்கப்படும் கேள்விகள்

Making A Large Island பின்னாலுள்ள intuition என்ன?

Label every island with union-find (tracking sizes). Then for each water cell, look at the distinct islands around it: flipping it joins them, giving 1 + their sizes. Also consider the grid being all land.

Making A Large Island (LeetCode 827) ஐ எப்படித் தீர்ப்பது?

1. Union adjacent land cells (index row·n + col). 2. For each 0, collect distinct neighbour roots and sum their sizes + 1. 3. Also take the largest existing island. 4. Return the maximum.

Making A Large Island இன் time மற்றும் space complexity என்ன?

Time: O(n²). Space: O(n²). (Accepted approach: DSU with sizes.)

Making A Large Island இல் பொதுவான தவறுகள் என்ன?

Use a set of neighbour roots so the same island isn't counted twice.