Making A Large Island
LeetCode 827 · कठीण · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू
DSU sizes + try flipping each 0.
मी 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
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.
मी कसा सोडवला
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_sizeMaking 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.