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.