Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Disjoint Sets solution in Clear category for Calculate Islands by 1-more
parent,size = [],[]
def find_set(v):
if v != parent[v]: parent[v] = find_set(parent[v])
return parent[v]
def union_sets(a, b):
a,b = find_set(a),find_set(b)
if a != b:
if size[a] < size[b]: a,b = b,a
parent[b] = a
size[a] += size[b]
def checkio(matrix):
N,M = len(matrix),len(matrix[0])
global parent,size
parent= [i for i in range(N*M)]
size= [1]*N*M
for x in range(N):
for y in range(M):
for dx,dy in ((-1,-1),(-1,0),(-1,1),(0,-1)):
if 0<=x+dx and 0<=y+dy
Nov. 11, 2015
Comments: