description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def prime_factors(num):
pfactors = []
factor = 2
while num >= factor * factor:
if num % factor == 0:
pfactors.append(factor)
num = num // factor
else:
factor += 1
pfactors.append(num)
return pfactors
class DisjointSetUnion(object):
def __init__(self, n):
self.parent = {i: i for i in range(n + 1)}
self.size = [1] * (1 + n)
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, a, b):
x = self.find(a)
y = self.find(b)
if x == y:
return x
if self.size[x] > self.size[y]:
self.parent[y] = x
self.size[x] += self.size[y]
return x
self.parent[x] = y
self.size[y] += self.size[x]
return y
DS = DisjointSetUnion(max(A))
num_id = defaultdict(int)
for num in A:
pf = list(set(prime_factors(num)))
num_id[num] = pf[0]
for i in range(len(pf) - 1):
DS.union(pf[i], pf[i + 1])
max_size = 0
group_count = defaultdict(int)
for num in A:
group_id = DS.find(num_id[num])
group_count[group_id] += 1
max_size = max(max_size, group_count[group_id])
return max_size | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = max(A)
ans = 0
uf = UF(n)
count = collections.Counter()
for a in A:
for num in range(2, int(sqrt(a) + 1)):
if a % num == 0:
uf.union(a, num)
uf.union(a, a // num)
for a in A:
pa = uf.find(a)
count[pa] += 1
ans = max(ans, count[pa])
return ans
class UF:
def __init__(self, n: int):
self.rank = [0] * (n + 1)
self.parent = [i for i in range(n + 1)]
def union(self, u: int, v: int) -> None:
pu = self.parent[u]
pv = self.parent[v]
if pu == pv:
return
if self.rank[pu] < self.rank[pv]:
self.parent[pu] = pv
elif self.rank[pv] < self.rank[pu]:
self.parent[pv] = pu
else:
self.parent[pu] = pv
self.rank[pv] += 1
def find(self, u: int) -> int:
while u != self.parent[u]:
self.parent[u] = self.parent[self.parent[u]]
u = self.parent[u]
return u | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NONE FUNC_DEF VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int], N=100001) -> int:
m = {}
P = [i for i in range(N)]
L = [1] * N
def find(x):
P[x] = P[x] if x == P[x] else find(P[x])
return P[x]
def union(a, b):
a = find(a)
b = find(b)
if a == b:
return
P[b] = a
L[a] += L[b]
for x in A:
m[x] = x if x not in m else m[x]
union(m[x], x)
for i in range(2, floor(sqrt(x)) + 1):
if x % i:
continue
j = x // i
m[i] = x if i not in m else m[i]
union(m[i], x)
m[j] = x if j not in m else m[j]
union(m[j], x)
return max(L) | CLASS_DEF FUNC_DEF VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, N):
self.data = list(range(N))
def union(self, x, y):
xp, yp = self.find(x), self.find(y)
self.data[xp] = yp
def find(self, x):
if self.data[x] != x:
self.data[x] = self.find(self.data[x])
return self.data[x]
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def primeFactors(n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return primeFactors(n // i) | set([i])
return set([n])
primes = collections.defaultdict(list)
for i, num in enumerate(A):
factors = primeFactors(num)
for factor in factors:
primes[factor].append(i)
dsu = DSU(len(A))
for factor, nums in primes.items():
for i in range(1, len(nums)):
dsu.union(nums[i - 1], nums[i])
counter = collections.Counter(dsu.find(i) for i in dsu.data)
return max(v for v in counter.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | def sieve(n):
if n < 3:
return
yield 2
p = set(range(5, n, 2))
i = 3
while i < n:
yield i
p.difference_update(list(range(i * i, n, i * 2)))
for i in range(i + 2, n, 2):
if i in p:
p.remove(i)
break
else:
return
class UnionFind:
def __init__(self, n):
self.uf = list(range(n))
self.size = defaultdict(lambda: 1)
def find(self, i):
if self.uf[i] != i:
self.uf[i] = self.find(self.uf[i])
return self.uf[i]
def union(self, a, b):
x, y = self.find(a), self.find(b)
if x == y:
return
self.uf[x] = self.uf[y]
self.size[y] += self.size[x]
self.size[x] = 0
PS = []
def factors(n):
if n % 2 == 0:
yield 2
while n % 2 == 0:
n //= 2
for i in range(3, int(n**0.5) + 2, 2):
if i > n:
break
if n % i == 0:
yield i
while n % i == 0:
n //= i
if n > 2:
yield n
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
if not A:
return 0
u = UnionFind(len(A))
x = {}
for i, a in enumerate(A):
for f in factors(a):
if f in x:
u.union(i, x[f])
x[f] = i
return max(u.size.values()) | FUNC_DEF IF VAR NUMBER RETURN EXPR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR NUMBER NUMBER EXPR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER EXPR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR VAR CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = max(A) + 1
parent = [i for i in range(n)]
rank = [0] * n
for a in A:
for k in range(2, int(sqrt(a)) + 1):
if a % k == 0:
self.union(parent, a, k, rank)
self.union(parent, a, a // k, rank)
counter = collections.Counter()
for a in A:
counter[self.find_root(parent, a)] += 1
return max(counter.values())
def find_root(self, parent, x):
if parent[x] != x:
parent[x] = self.find_root(parent, parent[x])
return parent[x]
def union(self, parent, x, y, rank):
x_root = self.find_root(parent, x)
y_root = self.find_root(parent, y)
if x_root != y_root:
if rank[x_root] < rank[y_root]:
parent[x_root] = y_root
elif rank[x_root] > rank[y_root]:
parent[y_root] = x_root
else:
parent[x_root] = y_root
rank[y_root] += 1
def find_common_factor(self, a, b):
for i in range(2, min(a, b) + 1):
if a % i == 0 and b % i == 0:
return True
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
if n < 2:
return n
A = set(A)
if 1 in A:
A.remove(1)
if len(A) == 0:
return 1
def list_primes(m):
p = [True] * (m + 1)
for i in range(2, m + 1):
if p[i]:
for j in range(2 * i, m + 1, i):
p[j] = False
return [i for i in range(2, m + 1) if p[i]]
def find_prime_factors(x, prime_list, prime_set):
if x in prime_set:
return [x]
prime_factors = []
for prime_num in prime_list:
if prime_num * prime_num > x:
break
if x % prime_num == 0:
prime_factors.append(prime_num)
while x % prime_num == 0:
x //= prime_num
if x != 1:
prime_factors.append(x)
return prime_factors
def prime_decompose(x):
f = 2
prime_factors = []
while x >= f * f:
if x % f == 0:
prime_factors.append(f)
x //= f
else:
f += 1
prime_factors.append(x)
return prime_factors
prime_list = list_primes(max(A))
prime_set = set(prime_list)
d = {x: find_prime_factors(x, prime_list, prime_set) for x in A}
all_factors = set()
for x in A:
for f in d[x]:
all_factors.add(f)
find_dict = {f: f for f in all_factors}
sink_size = {f: (1) for f in all_factors}
def find(f):
if find_dict[f] != f:
find_dict[f] = find(find_dict[f])
return find_dict[f]
def union(fi, fj):
sink_i = find(fi)
sink_j = find(fj)
if sink_i == sink_j:
return
if sink_size[sink_i] > sink_size[sink_j]:
find_dict[sink_j] = sink_i
sink_size[sink_i] += sink_size[sink_j]
else:
find_dict[sink_i] = sink_j
sink_size[sink_j] += sink_size[sink_i]
def union_hyperedge(hyperedge):
for i in range(1, len(hyperedge)):
union(hyperedge[i], hyperedge[0])
def union_no_load_balance(hyperedge):
sink = find(hyperedge[0])
for f in hyperedge:
find_dict[find(f)] = sink
find(f)
for x in A:
union_hyperedge(d[x])
sinks = set()
for f in all_factors:
sinks.add(find(f))
count = {sink: (0) for sink in sinks}
for x in A:
count[find(d[x][0])] += 1
return max(count.values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR RETURN LIST VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, n: int):
self.n = n
self.root = list(range(n))
self.size = [1] * n
self.max = 1
def find(self, x: int) -> int:
if self.root[x] != x:
self.root[x] = self.find(self.root[x])
return self.root[x]
def union(self, x: int, y: int):
rx, ry = self.find(x), self.find(y)
if not rx == ry:
self.root[rx] = ry
self.size[ry] += self.size[rx]
self.max = max(self.max, self.size[ry])
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
dsu = DSU(n)
seen = {}
for i, a in enumerate(A):
for j in range(2, int(math.sqrt(a)) + 1):
if a % j:
continue
if j in seen:
dsu.union(i, seen[j])
else:
seen[j] = i
if a // j in seen:
dsu.union(i, seen[a // j])
else:
seen[a // j] = i
if a in seen:
dsu.union(i, seen[a])
else:
seen[a] = i
return dsu.max | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def primeFactors(self, n):
pset = set()
while n % 2 == 0:
pset.add(2)
n = n // 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
pset.add(i)
n = n // i
if n > 2:
pset.add(n)
return pset
def largestComponentSize(self, A: List[int]) -> int:
ds = DisjointSet(A)
primeRoots = {}
for i, a in enumerate(A):
primes = self.primeFactors(a)
for p in primes:
if p in primeRoots:
ds.union(i, primeRoots[p])
primeRoots[p] = i
return max(ds.sizes)
class DisjointSet:
def __init__(self, elements):
self.parents = [x for x in range(len(elements))]
self.sizes = [1] * len(elements)
def find(self, x):
while x != self.parents[x]:
x, self.parents[x] = self.parents[x], self.parents[self.parents[x]]
return x
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px == py:
return px
if self.sizes[px] > self.sizes[py]:
py, px = px, py
self.parents[px] = py
self.sizes[py] += self.sizes[px] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | def union(parent, rank, i, j):
parI = find(parent, i)
parJ = find(parent, j)
if parI == parJ:
return
if rank[parI] < rank[parJ]:
parent[parI] = parJ
elif rank[parI] > rank[parJ]:
parent[parJ] = parI
else:
parent[parJ] = parI
rank[parI] += 1
def find(parent, i):
if i != parent[i]:
parent[i] = find(parent, parent[i])
return parent[i]
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
rank = [0] * n
parent = list(range(n))
factorToSet = {}
for i, num in enumerate(A):
for factor in range(2, int(math.sqrt(num) + 1)):
if num % factor == 0:
if factor in factorToSet:
union(parent, rank, i, factorToSet[factor])
else:
factorToSet[factor] = i
cfactor = num // factor
if cfactor in factorToSet:
union(parent, rank, i, factorToSet[cfactor])
else:
factorToSet[cfactor] = i
if num in factorToSet:
union(parent, rank, i, factorToSet[num])
else:
factorToSet[num] = i
for i in range(n):
find(parent, i)
return max(collections.Counter(parent).values()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def primeDecompose(self, num):
factor = 2
prime_factors = []
while num >= factor * factor:
if not num % factor:
prime_factors.append(factor)
num //= factor
else:
factor += 1
prime_factors.append(num)
return prime_factors
def largestComponentSize(self, A: List[int]) -> int:
factors = collections.defaultdict(set)
for num in A:
prime_factors = self.primeDecompose(num)
for p in prime_factors:
factors[p] |= set([num])
primes = list(factors)
group_id = 0
group = {}
prime_id = {}
updated = set(primes)
first_round = True
for i in range(len(primes)):
a = primes[i]
if a not in factors:
continue
for b in list(factors):
if b == a:
continue
if len(factors[b]) == 1:
del factors[b]
elif factors[a] & factors[b]:
factors[a] |= factors[b]
del factors[b]
return len(factors[max(factors, key=lambda f: len(factors[f]))]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A):
n = len(A)
primes = collections.defaultdict(list)
parents = [i for i in range(n)]
def find(x):
if parents[x] != x:
return find(parents[x])
return parents[x]
def union(x, y):
xr, yr = find(x), find(y)
if xr > yr:
parents[xr] = yr
else:
parents[yr] = xr
def primeSet(N):
for i in range(2, int(math.sqrt(N)) + 1):
if N % i == 0:
return {i} | primeSet(N // i)
return {N}
for i, num in enumerate(A):
for q in primeSet(num):
primes[q] += [i]
for _, indexes in primes.items():
for i in range(len(indexes) - 1):
union(indexes[i], indexes[i + 1])
return max(Counter([find(i) for i in range(n)]).values()) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR LIST VAR FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
record = [-1] * n
primes = collections.defaultdict(list)
def get_primes(i):
for j in range(2, int(math.sqrt(i) + 1)):
if i % j == 0:
return get_primes(i // j) | set([j])
return set([i])
return res
def find(a):
if record[a] < 0:
return a
record[a] = find(record[a])
return record[a]
def union(i, j):
a, b = find(i), find(j)
if a != b:
if a < b:
record[a] = b
else:
record[b] = a
for i, v in enumerate(A):
primes_set = get_primes(v)
for j in primes_set:
primes[j].append(i)
for _, indexes in list(primes.items()):
for i in range(len(indexes)):
union(indexes[0], indexes[i])
res = 0
mapping = collections.defaultdict(set)
for i in range(n):
root = find(i)
mapping[root].add(i)
if len(mapping[root]) > res:
res = len(mapping[root])
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR RETURN VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, nums):
self.nums = [i for i in range(len(nums))]
self.cnt = [1] * len(nums)
def find(self, idx):
if self.nums[idx] == idx:
return idx
pidx = self.find(self.nums[idx])
self.nums[idx] = pidx
return pidx
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px != py:
self.nums[px] = self.nums[py]
self.cnt[py] += self.cnt[px]
return self.cnt[py]
class Solution:
def primes_set(self, x):
for i in range(2, int(x**0.5) + 1):
if x % i == 0:
return self.primes_set(x // i) | set([i])
return set([x])
def largestComponentSize(self, A: List[int]) -> int:
dsu = DSU(A)
primes = {}
for i, x in enumerate(A):
for prime in self.primes_set(x):
if prime not in primes:
primes[prime] = []
primes[prime].append(i)
ans = 0
for _, idxs in list(primes.items()):
for i in range(len(idxs) - 1):
ans = max(ans, dsu.union(idxs[i], idxs[i + 1]))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Node:
def __init__(self, data):
self.data = data
self.p = None
class Solution:
def find(self, node):
if node.p == node:
return node
node.p = self.find(node.p)
return node.p
def prime(self, val, d):
for i in range(2, int(val**0.5) + 1):
if val % i == 0:
if i not in d:
node = Node(i)
node.p = node
d[i] = node
if self.find(d[val].p) == d[val]:
d[val].p = d[i]
else:
self.find(d[i]).p = self.find(d[val])
v = val // i
if v != i:
if v not in d:
node = Node(v)
node.p = node
d[v] = node
self.find(d[v]).p = self.find(d[val])
def largestComponentSize(self, A: List[int]) -> int:
d = {}
if not A:
return 0
for i in A:
if i not in d:
node = Node(i)
node.p = node
d[i] = node
self.prime(i, d)
fr = defaultdict(int)
for i in A:
fr[self.find(d[i]).data] += 1
mx = 0
for i in fr.values():
mx = max(i, mx)
return mx | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR DICT IF VAR RETURN NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
graph = defaultdict(list)
uf = UnionFind(len(A))
for i, num in enumerate(A):
prime_factors = set(self.get_prime_factors(num))
for factor in prime_factors:
graph[factor].append(i)
for factor in graph:
indices = graph[factor]
for i in range(len(indices) - 1):
uf.union(indices[i], indices[i + 1])
counter = Counter()
for i in range(len(A)):
root = uf.find(i)
counter[root] += 1
return max(counter.values())
def get_prime_factors(self, num):
res = []
factor = 2
while num >= factor * factor:
if num % factor == 0:
res.append(factor)
num = num // factor
else:
factor += 1
res.append(num)
return res
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def union(self, x, y):
rootx = self.find(x)
rooty = self.find(y)
if rootx == rooty:
return False
self.parent[rooty] = rootx
return True
def find(self, x):
while x != self.parent[x]:
self.parent[x] = self.parent[self.parent[x]]
x = self.parent[x]
return x | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class UnionFindSet:
def __init__(self, n):
self.parants = [0] * n
self.rank = [0] * n
for i in range(n):
self.parants[i] = i
def find(self, x):
if x != self.parants[x]:
self.parants[x] = self.find(self.parants[x])
return self.parants[x]
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px == py:
return False
if self.rank[px] < self.rank[py]:
self.parants[px] = py
elif self.rank[px] > self.rank[py]:
self.parants[py] = px
else:
self.parants[py] = px
self.rank[py] += 1
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
s = UnionFindSet(max(A) + 1)
for x in A:
for i in range(2, int(math.sqrt(x)) + 1):
if x % i == 0:
s.union(x, i)
s.union(x, x // i)
hash = defaultdict(int)
for x in A:
hash[s.find(x)] += 1
return max(hash.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DS:
def __init__(self, N: int):
self.p = list(range(N))
self.size = [1] * N
self.max = 1
def find(self, node: int) -> int:
if self.p[node] != node:
self.p[node] = self.find(self.p[node])
return self.p[node]
def union(self, x: int, y: int) -> None:
px, py = self.find(x), self.find(y)
if px != py:
sx, sy = self.size[px], self.size[py]
if sx < sy:
self.p[px] = py
self.size[py] += sx
self.max = max(self.max, self.size[py])
else:
self.p[py] = px
self.size[px] += sy
self.max = max(self.max, self.size[px])
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
factor_index = {}
UF = DS(len(A))
for idx, num in enumerate(A):
for factor in range(2, int(num**0.5 + 1)):
if num % factor == 0:
for fact in (factor, num // factor):
if fact in factor_index:
UF.union(factor_index[fact], idx)
else:
factor_index[fact] = idx
if num in factor_index:
UF.union(factor_index[num], idx)
else:
factor_index[num] = idx
return UF.max | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NONE CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, n):
self.parent = [i for i in range(n)]
self.rank = [(0) for _ in range(n)]
self.size = n
def find(self, x):
if x != self.parent[x]:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
xp, yp = self.find(x), self.find(y)
if xp == yp:
return False
if self.rank[xp] < self.rank[yp]:
self.parent[xp] = yp
elif self.rank[xp] > self.rank[yp]:
self.parent[yp] = xp
else:
self.parent[xp] = yp
self.rank[yp] += 1
self.size -= 1
return True
def getSize(self):
return self.size
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def getFactors(n):
res = set()
for i in range(2, int(sqrt(n)) + 2):
if not n % i:
for j in [i, n // i]:
if j != 1:
res.add(j)
if not res:
return {n}
return res
dsu = DSU(len(A) + 1)
factor2Index = {}
for i, n in enumerate(A):
for factor in getFactors(n):
if factor in factor2Index:
dsu.union(i, factor2Index[factor])
factor2Index[factor] = i
groupSize = collections.defaultdict(int)
for i in range(len(A)):
groupSize[dsu.find(i)] += 1
return max(groupSize.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF RETURN VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR FOR VAR LIST VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def __init__(self):
self.prime = [
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
167,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
]
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
factors_2_nodes = dict()
B = [[] for i in range(0, n)]
for i in range(0, n):
num = A[i]
found_factor = True
while num > 1 and found_factor:
found_factor = False
limit = int(sqrt(num + 1))
for f in self.prime:
if f > limit:
break
if num % f == 0:
if f not in B[i]:
B[i].append(f)
if f in factors_2_nodes:
factors_2_nodes[f].append(i)
else:
factors_2_nodes[f] = [i]
num //= f
found_factor = True
break
if not found_factor and num not in B[i]:
B[i].append(num)
if num in factors_2_nodes:
factors_2_nodes[num].append(i)
else:
factors_2_nodes[num] = [i]
v = [False] * n
m = 0
for i in range(0, n):
if v[i]:
continue
queue = [i]
c = 0
while queue:
q = queue.pop()
if v[q]:
continue
v[q] = True
c += 1
for f in B[q]:
for node in factors_2_nodes[f]:
if not v[node]:
queue.append(node)
factors_2_nodes[f] = []
if c > m:
m = c
return m | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, nums: List[int]) -> int:
uf = {}
def find(x):
if uf[x] != x:
uf[x] = find(uf[x])
return uf[x]
def union(x, y):
uf.setdefault(x, x)
uf.setdefault(y, y)
uf[find(x)] = uf[find(y)]
for num in nums:
union(num, num)
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
union(num, i)
union(num // i, i)
group_counter = collections.Counter()
for num in nums:
group_counter[find(num)] += 1
return max(group_counter.values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DisjointSet:
def __init__(self, size):
self.parents = list(range(size))
self.sizes = [1] * size
def find(self, x):
if self.parents[x] != x:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
parent_x = self.find(x)
parent_y = self.find(y)
if parent_x != parent_y:
self.sizes[parent_x] += self.sizes[parent_y]
self.parents[parent_y] = parent_x
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
N = len(A)
def to_divisors(n):
if n % 6 == 0:
return [2, 3]
if n % 3 == 0:
return [3]
if n % 2 == 0:
return [2]
return []
maxA = max(A)
divisors = [to_divisors(i) for i in range(maxA + 1)]
for i in range(4, maxA + 1):
if len(divisors[i]) == 0:
for j in range(i, maxA + 1, i):
divisors[j].append(i)
disjoint_set = DisjointSet(N)
divisor_to_index = {}
for i, a in enumerate(A):
for d in divisors[a]:
if d in divisor_to_index:
disjoint_set.union(i, divisor_to_index[d])
divisor_to_index[d] = i
return max(disjoint_set.sizes) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN LIST NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN LIST NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def primeDecompose(self, num):
factor = 2
prime_factors = []
while num >= factor * factor:
if num % factor == 0:
prime_factors.append(factor)
num = num // factor
else:
factor += 1
prime_factors.append(num)
return prime_factors
def largestComponentSize(self, A: List[int]) -> int:
factorMap = {}
def find(a):
if a not in factorMap or factorMap[a] == a:
factorMap[a] = a
return a
else:
return find(factorMap[a])
factorCount = {}
maxCount = 0
for n in A:
primeFactors = self.primeDecompose(n)
pivot = find(primeFactors[0])
if pivot not in factorCount:
factorCount[pivot] = 0
factorCount[pivot] += 1
for i in range(1, len(primeFactors)):
root = find(primeFactors[i])
factorMap[root] = pivot
if root in factorCount and root != pivot:
factorCount[pivot] += factorCount[root]
maxCount = max(maxCount, factorCount[pivot])
return maxCount | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DisjointSet:
def __init__(self, n):
self.parent = [i for i in range(n)]
self.rank = [(0) for i in range(n)]
def GetParent(self, i):
if self.parent[i] != i:
self.parent[i] = self.GetParent(self.parent[i])
return self.parent[i]
def Union(self, i, j):
i_idx = self.GetParent(i)
j_idx = self.GetParent(j)
if j_idx == i_idx:
return
if self.rank[i_idx] > self.rank[j_idx]:
self.parent[j_idx] = i_idx
else:
if self.rank[i_idx] == self.rank[j_idx]:
self.rank[j_idx] += 1
self.parent[i_idx] = j_idx
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def prime_set(n):
for i in range(2, math.floor(math.sqrt(n)) + 1):
if n % i == 0:
return prime_set(n // i) | set([i])
return set([n])
ds = DisjointSet(len(A))
primes = {}
for i in range(len(A)):
curr_prime_set = prime_set(A[i])
for prime in curr_prime_set:
primes.setdefault(prime, [])
primes[prime].append(i)
for key in primes:
for i in range(len(primes[key]) - 1):
ds.Union(primes[key][i], primes[key][i + 1])
set_counter = {}
for i in range(len(A)):
set_counter.setdefault(ds.GetParent(i), 0)
set_counter[ds.GetParent(i)] += 1
return max(set_counter.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def get_primes_set(n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return get_primes_set(n // i) | set([i])
return set([n])
A = set(A)
primes = set()
children_of = collections.defaultdict(set)
for a in A:
ap = get_primes_set(a)
prev = None
for pr in ap:
primes.add(pr)
children_of[pr].add(a)
if prev is not None:
children_of[pr].add(prev)
children_of[prev].add(pr)
prev = pr
print(children_of)
seen = set()
sizes = dict()
for pr in primes:
if pr in seen:
continue
sizes[pr] = 0
to_explore = collections.deque([pr])
while to_explore:
curr = to_explore.popleft()
if curr in seen:
continue
seen.add(curr)
sizes[pr] += curr in A
for child in children_of[curr]:
to_explore.append(child)
return max(sizes.values()) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def sieve():
i = 2
while i < len(spf):
spf[i] = 2
i += 2
i = 3
while i * i < len(spf):
if spf[i] == i:
j = i
while j < len(spf):
if spf[j] == j:
spf[j] = i
j += i
i += 2
spf, di, factors, visited_nums, visited_factors, ans = (
[i for i in range(max(A) + 1)],
defaultdict(set),
defaultdict(set),
set(),
set(),
1,
)
sieve()
for num in A:
x = num
while x != 1:
di[spf[x]].add(num)
factors[num].add(spf[x])
x //= spf[x]
for num in A:
if num in visited_nums:
continue
visited_nums.add(num)
cur, queue = 1, deque([])
for factor in factors[num]:
queue.append(factor)
while queue:
factor = queue.popleft()
visited_factors.add(factor)
for next_num in di[factor]:
if next_num in visited_nums:
continue
visited_nums.add(next_num)
cur += 1
for next_factor in factors[next_num]:
if next_factor in visited_factors:
continue
visited_factors.add(next_factor)
queue.append(next_factor)
ans = max(ans, cur)
return ans | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class UFDS:
def __init__(self, n, nums):
self.p = list(range(n + 1))
self.size = [0] * (n + 1)
for num in nums:
self.size[num] = 1
def find(self, n):
if n != self.p[n]:
self.p[n] = self.find(self.p[n])
return self.p[n]
def union(self, i, j):
p1 = self.find(i)
p2 = self.find(j)
if p1 != p2:
if self.size[p1] > self.size[p2]:
self.p[p2] = p1
self.size[p1] += self.size[p2]
else:
self.p[p1] = p2
self.size[p2] += self.size[p1]
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
hi = max(A)
ufds = UFDS(hi, A)
isPrime = [True] * (hi + 1)
for i in range(2, hi + 1):
if not isPrime[i]:
continue
for j in range(2 * i, hi + 1, i):
isPrime[j] = False
if ufds.size[j]:
ufds.union(i, j)
return max(ufds.size) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
rank = collections.defaultdict(int)
parent = collections.defaultdict(int)
def find(a):
if parent[a] == 0:
return a
if parent[a] != a and parent[a] != 0:
parent[a] = find(parent[a])
return parent[a]
def union(a, b):
a = find(a)
b = find(b)
if rank[a] > rank[b]:
parent[b] = a
rank[a] += 1
elif rank[a] <= rank[b]:
parent[a] = b
rank[b] += 1
def prime_set(n):
for i in range(2, int(sqrt(n)) + 1):
if n % i == 0:
return prime_set(n // i) | set([i])
return set([n])
primes = defaultdict(list)
for i, num in enumerate(A):
ps = prime_set(num)
for p in ps:
primes[p].append(i)
print(primes)
for p in primes:
pairs = primes[p]
for i in range(len(primes[p]) - 1):
union(A[pairs[i]], A[pairs[i + 1]])
print(Counter(find(A[i]) for i in range(len(A))))
return max(Counter(find(A[i]) for i in range(len(A))).values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class UnionFind:
def __init__(self, nums):
self.nums = nums
self.groups = list(range(len(nums)))
self.sizes = [1] * len(nums)
def union(self, a, b):
a, b = self.find(a), self.find(b)
if a == b:
return
if self.sizes[a] < self.sizes[b]:
a, b = b, a
self.sizes[a] += self.sizes[b]
self.groups[b] = self.groups[a]
def find(self, a):
head = self.groups[a]
while head != self.groups[head]:
head = self.groups[head]
node = self.groups[a]
while node != self.groups[node]:
node, self.groups[node] = self.groups[node], head
return head
def largest(self):
return max(self.sizes)
class Solution:
def largestComponentSize(self, a: List[int]) -> int:
def factors(n):
ans = {n}
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
ans.add(i)
ans.add(n // i)
return ans
uf = UnionFind(a)
primes = defaultdict(list)
for i, num in enumerate(a):
pr_set = factors(num)
for prime in pr_set:
primes[prime].append(i)
for prime, idxs in primes.items():
for i in range(len(idxs) - 1):
uf.union(idxs[i], idxs[i + 1])
return uf.largest() | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, n):
self.parent = [i for i in range(n)]
self.rank = [(1) for i in range(n)]
def find(self, x):
if x != self.parent[x]:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
px = self.find(x)
py = self.find(y)
if px == py:
return
if self.rank[px] > self.rank[py]:
self.parent[py] = px
self.rank[px] += self.rank[py]
else:
self.parent[px] = py
self.rank[py] += self.rank[px]
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
dsu = DSU(n)
primes = defaultdict(list)
for i, num in enumerate(A):
pr_set = self.primes_set(num)
for q in pr_set:
primes[q].append(i)
for _, indexes in list(primes.items()):
for i in range(len(indexes) - 1):
dsu.union(indexes[i], indexes[i + 1])
return max(dsu.rank)
def primes_set(self, n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return self.primes_set(n // i) | set([i])
return set([n])
def gcd(self, x, y):
return gcd(y, x % y) if y != 0 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR FUNC_DEF RETURN VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class UF:
def __init__(self, n):
self.p = [i for i in range(n)]
self.s = [1] * n
def find(self, x):
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
xr, yr = self.find(x), self.find(y)
self.s[xr] += self.s[yr]
self.p[yr] = xr
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def add_prime(i, j, primes):
if i not in primes:
primes[i] = [j]
else:
primes[i].append(j)
g = UF(len(A))
primes = {}
for j, k in enumerate(A):
i = 2
while i * i <= k:
if k % i == 0:
add_prime(i, j, primes)
while k % i == 0:
k = k // i
i += 1
if k > 1:
add_prime(k, j, primes)
for l in primes.values():
j, r = l[0], g.find(l[0])
for i in l[1:]:
if g.find(i) != r:
g.union(j, i)
return max(g.s) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class UnionFind:
def __init__(self, nodes):
self.parents = [-1] * len(nodes)
self.map = {node: idx for idx, node in enumerate(nodes)}
def find(self, node):
idx = self.map[node]
orig = idx
while self.parents[idx] >= 0:
idx = self.parents[idx]
parent, rank = idx, abs(self.parents[idx])
while self.parents[orig] >= 0:
temp_parent = self.parents[orig]
self.parents[orig] = parent
orig = temp_parent
return parent, rank
def union(self, n1, n2):
idx1, idx2 = self.map[n1], self.map[n2]
parent1, rank1 = self.find(n1)
parent2, rank2 = self.find(n2)
if parent1 == parent2:
return
if rank1 >= rank2:
self.parents[parent1] -= rank2
self.parents[parent2] = parent1
else:
self.parents[parent2] -= rank1
self.parents[parent1] = parent2
def get_max_rank(self):
return abs(min(self.parents))
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
max_num = max(A)
primes_nodes = collections.defaultdict(set)
def prime_factors(n):
original = n
result = set()
while n % 2 == 0:
n = n // 2
result.add(2)
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
result.add(i)
n = n // i
if n > 2:
result.add(n)
return result
uf_obj = UnionFind(A)
factor_first_multiple_map = {}
for node in A:
p_factors = prime_factors(node)
for pf in p_factors:
if pf in factor_first_multiple_map:
uf_obj.union(node, factor_first_multiple_map[pf])
else:
factor_first_multiple_map[pf] = node
return uf_obj.get_max_rank() | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
dsu = DSU(n)
prime_dict = defaultdict(list)
for i, num in enumerate(A):
primes = self.find_p(num)
for p in primes:
prime_dict[p].append(i)
for _, p_list in list(prime_dict.items()):
for j in range(len(p_list)):
dsu.union(p_list[0], p_list[j])
return max(Counter(dsu.find(x) for x in range(n)).values())
def find_p(self, x):
for i in range(2, int(math.sqrt(x)) + 1):
if x % i == 0:
return self.find_p(x // i) | set([i])
return set([x])
class DSU:
def __init__(self, n):
self.sets = list(range(n))
def find(self, x):
if self.sets[x] != x:
root = self.find(self.sets[x])
self.sets[x] = root
return root
return x
def union(self, x, y):
xr, yr = self.find(x), self.find(y)
self.sets[yr] = xr | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
class UF:
def __init__(self, N):
self.parents = [i for i in range(N)]
self.sizes = [(1) for _ in range(N)]
self.max = 1
def find(self, x):
if self.parents[x] != x:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if self.sizes[x] < self.sizes[y]:
x, y = y, x
if x != y:
self.parents[y] = x
self.sizes[x] += self.sizes[y]
self.max = max(self.max, self.sizes[x])
uf = UF(len(A))
dic = {}
for i, num in enumerate(A):
for factor in range(2, int(math.sqrt(num) + 1)):
if num % factor == 0:
for fac in [factor, num // factor]:
if fac in dic:
uf.union(dic[fac], i)
else:
dic[fac] = i
if num in dic:
uf.union(dic[num], i)
else:
dic[num] = i
return uf.max | CLASS_DEF FUNC_DEF VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR LIST VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Union:
def __init__(self, arr):
self.weight = {}
self.par = {}
for a in range(1, max(arr) + 1):
self.par[a] = a
self.weight[a] = 1
def find(self, a):
while self.par[a] != a:
a = self.par[a]
return a
def union(self, a, b):
pa, pb = self.find(a), self.find(b)
if pa == pb:
return
if self.weight[pa] < self.weight[pb]:
pa, pb = pb, pa
self.weight[pa] += self.weight[pb]
self.par[pb] = pa
def getMaxWeight(self):
return max(list(map(len, list(self.weight.values()))))
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
union = Union(A)
for a in A:
sqr = int(sqrt(a))
for j in range(2, sqr + 1):
if a % j == 0:
union.union(a, a // j)
union.union(a, j)
groups = collections.defaultdict(int)
for a in A:
groups[union.find(a)] += 1
return max(groups.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class ds:
def __init__(self, n):
self.arr = list(range(n))
self.size = [1] * n
def find(self, x):
if self.arr[x] != x:
self.arr[x] = self.find(self.arr[x])
return self.arr[x]
def union(self, x, y):
Ax = self.find(x)
Ay = self.find(y)
if Ax == Ay:
return False
if self.size[Ax] > self.size[Ay]:
Ax, Ay = Ay, Ax
self.arr[Ax] = Ay
self.size[Ay] += self.size[Ax]
return True
def primes(n):
odds = range(3, n + 1, 2)
sieve = set(sum([list(range(q * q, n + 1, q + q)) for q in odds], []))
return [2] + [p for p in odds if p not in sieve]
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def factors(n):
result = set()
for p in primeNumber:
while n % p == 0:
n //= p
result.add(p)
if n > 1:
result.add(n)
return list(result)
n, m = len(A), max(A)
dsa = ds(m + 1)
primeNumber = primes(round(m**0.5) + 1)
for d in A:
rest = factors(d)
for i in rest:
dsa.union(d, i)
return Counter([dsa.find(x) for x in A]).most_common(1)[0][1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR LIST RETURN BIN_OP LIST NUMBER VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class UF:
def __init__(self) -> None:
self.parent = {}
self.sz = {}
self.count = 0
def add(self, p):
if p not in self.parent:
self.parent[p] = p
self.sz[p] = 1
self.count += 1
def find(self, p: int) -> int:
while p != self.parent[p]:
self.parent[p] = self.parent[self.parent[p]]
p = self.parent[p]
return p
def union(self, p: int, q: int) -> None:
i = self.find(p)
j = self.find(q)
if i == j:
return
if self.sz[i] > self.sz[j]:
self.parent[j] = i
self.sz[i] += self.sz[j]
else:
self.parent[i] = j
self.sz[j] += self.sz[i]
self.count -= 1
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def fac(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
n //= i
return set((i,)) | fac(n)
return set((n,))
@lru_cache(None)
def group_id(p):
return uf.find(p)
uf = UF()
c = Counter()
for n in A:
factors = fac(n)
key = tuple(sorted(factors))
c[key] += 1
if c[key] == 1:
p = factors.pop()
uf.add(p)
for q in factors:
uf.add(q)
uf.union(p, q)
if uf.count == 1:
return len(A)
groups = Counter()
for factors, value in c.items():
groups[group_id(factors[0])] += value
return groups.most_common()[0][1] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER NONE FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_DEF VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER NONE CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | isp = [(True) for i in range(100000)]
pr = []
for i in range(2, 100000):
c = i
if isp[i]:
pr.append(i)
while c < 100000 - i:
c += i
isp[c] = False
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def pcr(n):
r = n**0.5
ans = {n}
for p in pr:
if p > r:
if n > 1:
ans.add(n)
break
if n % p == 0:
ans.add(p)
while n % p == 0:
n = n // p
r = n**0.5
return ans
def iscf(n1, n2):
nn1, nn2 = max(n1, n2), min(n1, n2)
while nn1 % nn2 != 0:
n1 = nn1 - nn2
n2 = nn2
nn1, nn2 = max(n1, n2), min(n1, n2)
n = min(nn1, nn2)
return True if n > 1 else False
prn = {a: pcr(a) for a in A}
print(prn)
while True:
rms = set()
ks = list(prn.keys())
kn = len(ks)
for i, k in enumerate(ks):
if len(prn[k]) == 1:
rms.add(k)
continue
for j in range(i + 1, kn):
if len(prn[ks[j]]) == 1:
rms.add(ks[j])
continue
if prn[k].intersection(prn[ks[j]]):
prn[k].update(prn[ks[j]])
rms.add(ks[j])
if rms:
break
for k in rms:
del prn[k]
if not rms:
break
aa = set(A)
cdd = [len(v & aa) for v in prn.values()]
return max(cdd) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FOR VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DisjSet:
def __init__(self, n):
self.disj_set = [-1] * n
def find(self, x):
while self.disj_set[x] >= 0:
x = self.disj_set[x]
return x
def union(self, x, y):
i = self.find(x)
j = self.find(y)
if i == j:
return
if self.disj_set[i] < self.disj_set[j]:
self.disj_set[i] += self.disj_set[j]
self.disj_set[j] = i
else:
self.disj_set[j] += self.disj_set[i]
self.disj_set[i] = j
def maxset(self):
return -min(self.disj_set)
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def gen_primes(n):
candidates = [1] * n
for i in range(2, int(math.sqrt(n)) + 1):
if candidates[i]:
for j in range(i**2, n, i):
candidates[j] = 0
return [i for i in range(2, n) if candidates[i]]
primes = gen_primes(int(math.sqrt(max(A))) + 1)
def factor(n):
result = set()
for i in primes:
if i > int(math.sqrt(n)):
break
if n % i == 0:
result.add(i)
while n % i == 0:
n //= i
result.add(n)
return result
n = len(A)
disj = DisjSet(n)
g = {}
for i, a in enumerate(A):
factors = factor(a)
for f in factors:
if f == 1:
continue
if f not in g:
g[f] = i
else:
disj.union(i, g[f])
return disj.maxset() | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, size):
self.indexes = {i: i for i in range(size)}
self.sizes = {i: (1) for i in range(size)}
def root(self, i):
node = i
while i != self.indexes[i]:
i = self.indexes[i]
while node != i:
nnode = self.indexes[node]
self.indexes[node] = i
node = nnode
return i
def unite(self, i, j):
ri, rj = self.root(i), self.root(j)
if ri == rj:
return
else:
self.indexes[rj] = ri
self.sizes[ri] += self.sizes[rj]
class Solution:
def primeSet(self, n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return self.primeSet(n // i) | set([i])
return set([n])
def largestComponentSize(self, A: List[int]) -> int:
d = collections.defaultdict(list)
for i in range(len(A)):
s = self.primeSet(A[i])
for v in s:
d[v].append(i)
dsu = DSU(len(A))
for k in d:
for ind in d[k][1:]:
dsu.unite(d[k][0], ind)
ans = 1
for i in range(len(A)):
ans = max(ans, dsu.sizes[i])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR RETURN FUNC_CALL VAR LIST VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
dic = defaultdict(list)
def primefactor(x):
s = []
y = x
if x % 2 == 0:
s.append(2)
while x % 2 == 0:
x //= 2
for i in range(3, floor(sqrt(y)) + 1, 2):
if x % i == 0:
s.append(i)
while x % i == 0:
x //= i
if x > sqrt(y):
s.append(x)
return s
for i in range(n):
primes = primefactor(A[i])
for d in primes:
dic[d].append(i)
a = list(range(n))
sz = [1] * n
def root(i):
while i != a[i]:
a[i] = a[a[i]]
i = a[i]
return i
def find(i, j):
return root(i) == root(j)
def union(i, j):
root_i = root(i)
root_j = root(j)
if sz[root_i] > sz[root_j]:
a[root_j] = root_i
sz[root_i] += sz[root_j]
sz[root_j] = 0
else:
a[root_i] = root_j
sz[root_j] += sz[root_i]
sz[root_i] = 0
for v in dic.values():
for i in range(len(v) - 1):
if find(v[i], v[i + 1]) == False:
union(v[i], v[i + 1])
return max(sz) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
primes = [
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
167,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
]
unions = collections.defaultdict(set)
for i, n in enumerate(A):
for p in primes:
if p * p > n:
break
while not n % p:
n //= p
unions[p].add(i)
if n > 1:
unions[n].add(i)
ids = list(range(len(A)))
def find(i):
if ids[i] != i:
ids[i] = find(ids[i])
return ids[i]
def union(l):
s = set(find(i) for i in l)
i = s.pop()
while s:
j = s.pop()
ids[j] = i
for l in list(unions.values()):
union(l)
c = collections.Counter(find(i) for i in range(len(A)))
return max(c.values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
amax = max(A)
plist = [2]
for k in range(3, int(sqrt(amax)) + 2, 2):
for p in plist:
if p * p > k:
plist.append(k)
break
if k % p == 0:
break
else:
plist.append(k)
pset = set(plist)
amax //= 2
n = n1 = n2 = len(A)
k = 0
while k < n1:
x = A[k]
if x in pset:
n1 -= 1
n2 -= 1
A[k] = A[n1]
A[n2] = x
continue
if x < plist[-1]:
k += 1
continue
prime = True
for p in plist:
if p * p > x:
break
if x % p == 0:
prime = False
break
if not prime:
k += 1
else:
n1 -= 1
A[k] = A[n1]
if x <= amax:
n2 -= 1
A[n2] = x
ans = 1
left = n - n2 + n1
while left > ans and n1 > 0:
n1 -= 1
x = A[n1]
found = False
k = 0
while k < n1:
y = A[k]
p = math.gcd(x, y)
if p > 1:
found = True
n1 -= 1
A[k] = A[n1]
while p > 1:
y //= p
p = math.gcd(y, p)
x *= y
else:
k += 1
if k == n1 and found:
found = False
k = 0
k = n - 1
while k >= n2:
if x % A[k] == 0:
A[k] = A[n2]
n2 += 1
else:
k -= 1
k = n - n2 + n1
ans = max(ans, left - k)
left = k
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, N):
self.p = list(range(N))
def find(self, x):
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
xr = self.find(x)
yr = self.find(y)
self.p[xr] = yr
class Solution(object):
def largestComponentSize(self, A):
B = []
for x in A:
facs = []
d = 2
while d * d <= x:
if x % d == 0:
while x % d == 0:
x /= d
facs.append(d)
d += 1
if x > 1 or not facs:
facs.append(x)
B.append(facs)
primes = list({p for facs in B for p in facs})
prime_to_index = {p: i for i, p in enumerate(primes)}
dsu = DSU(len(primes))
for facs in B:
for x in facs:
dsu.union(prime_to_index[facs[0]], prime_to_index[x])
count = collections.Counter(dsu.find(prime_to_index[facs[0]]) for facs in B)
return max(count.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
parent = {}
size = defaultdict(lambda: 1)
def ds(a):
while a in parent:
a = parent[a]
return a
def union(a, b):
if (a := ds(a)) != (b := ds(b)):
if size[a] < size[b]:
a, b = b, a
parent[b] = a
size[a] += size[b]
return a
for a in A:
for d in factors(a):
union(-d, a)
return Counter(ds(a) for a in A).most_common(1)[0][1]
primes = [
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
167,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
]
FS = {n: {n} for n in primes}
FS[1] = {}
def factors(f):
if (n := f) not in FS:
if n & 1:
FS[f] = factors3(f)
else:
n >>= 1
while n & 1 == 0:
n >>= 1
if n == 1:
FS[f] = {2}
else:
FS[f] = {2} | factors3(n)
return FS.get(f, set())
def factors3(f, start=3):
if (n := f) not in FS:
for i in range(start, int(sqrt(n)) + 1, 2):
if n % i == 0:
n = n // i
while n % i == 0:
n = n // i
if n == 1:
FS[f] = {i}
else:
FS[f] = {i} | factors3(n, i + 2)
break
else:
FS[f] = {f}
return FS[f] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER DICT FUNC_DEF IF VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
fa = [i for i in range(len(A))]
sz = [1] * len(A)
def find(x):
if fa[x] != x:
fa[x] = find(fa[x])
return fa[x]
factor = defaultdict(list)
for i in range(len(A)):
x = A[i]
for y in range(2, int(math.sqrt(x)) + 1):
if not x % y:
factor[y].append(i)
while not x % y:
x //= y
if x > 1:
factor[x].append(i)
for k, v in list(factor.items()):
for j in range(1, len(v)):
x, y = v[0], v[j]
fx, fy = find(x), find(y)
if fx != fy:
if sz[fx] > sz[fy]:
sz[fy] += sz[fx]
fa[fx] = fy
else:
sz[fx] += sz[fy]
fa[fy] = fx
ans = 0
for i in range(len(fa)):
if i == fa[i]:
ans = max(ans, sz[i])
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
parent = [i for i in range(max(A) + 1)]
def find(x):
if parent[x] == x:
return x
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
px = find(x)
py = find(y)
if px == py:
return px
parent[py] = px
return px
for a in A:
for k in range(2, int(sqrt(a)) + 1, 1):
if a % k == 0:
union(a, k)
union(a, a // k)
max_len = 0
d = defaultdict(int)
for a in A:
group_id = find(a)
d[group_id] += 1
max_len = max(max_len, d[group_id])
return max_len | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR RETURN VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class DSU:
def __init__(self, n):
self.parent = list(range(n))
def find(self, target):
if self.parent[target] != target:
self.parent[target] = self.find(self.parent[target])
return self.parent[target]
def union(self, c, p):
self.parent[self.find(c)] = self.find(p)
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
dsu = DSU(len(A))
def prime_factorize(n):
for i in range(2, int(sqrt(n)) + 1):
if n % i == 0:
return [i] + prime_factorize(n // i)
return [n]
prime2indices = defaultdict(list)
for i, n in enumerate(A):
for p in prime_factorize(n):
prime2indices[p].append(i)
for arr in list(prime2indices.values()):
for i in range(1, len(arr)):
dsu.union(arr[0], arr[i])
aggregate = defaultdict(int)
for i in range(len(A)):
aggregate[dsu.find(i)] += 1
return max(aggregate.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
parent = dict()
def find(x):
parent.setdefault(x, x)
if x != parent[x]:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
px = find(x)
py = find(y)
parent[px] = py
visited = set()
graph = collections.defaultdict(set)
count = 0
for a in A:
for fact in range(2, int(sqrt(a)) + 1):
if a % fact == 0:
x = a
y = fact
z = a // fact
px = find(a)
py = find(y)
pz = find(z)
if px != py:
union(x, y)
if px != pz:
union(x, z)
mem = collections.Counter()
for item in A:
p = find(item)
mem[p] += 1
maxVal = mem.most_common()
return maxVal[0][1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN VAR NUMBER NUMBER VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def pfactorization(a):
N = a
if a >= 2 and a % 2 == 0:
Gp[2].append(N)
Gn[N].append(2)
while a % 2 == 0:
a //= 2
for d in range(3, math.floor(a ** (1 / 2)) + 1, 2):
if d > a:
break
if a % d == 0:
Gn[N].append(d)
Gp[d].append(N)
while a % d == 0:
a //= d
if a > 1:
Gp[a].append(N)
Gn[N].append(a)
def dfs(curr):
if curr in seenNodes:
return 0
seenNodes.add(curr)
total = 1
for p in Gn[curr]:
if p in seenPrimes:
continue
seenPrimes.add(p)
for owner in Gp[p]:
total += dfs(owner)
return total
Gp = collections.defaultdict(list)
Gn = collections.defaultdict(list)
for a in A:
pfactorization(a)
seenPrimes, seenNodes = set(), set()
return max([dfs(a) for a in A]) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
parents = {val: val for val in A}
sizes = {val: (1) for val in A}
factorprez = {}
maxsize = 0
def union(one, two):
r1 = findroot(one)
r2 = findroot(two)
if r1 == r2:
return sizes[r1]
big, small = (r1, r2) if sizes[r1] > sizes[r2] else (r2, r1)
parents[small] = big
sizes[big] += sizes[small]
return sizes[big]
def findroot(node):
if parents[node] != node:
node = findroot(parents[node])
return parents[node]
def primes_before(val):
candidates = [True] * (val + 1)
primes = []
for i in range(2, len(candidates)):
if not candidates[i]:
continue
primes.append(i)
for j in range(i, val + 1, i):
candidates[j] = False
return primes
primes = primes_before(100000)
primeset = set(primes)
for val in A:
sval = val
prime_idx = 0
while sval not in primeset and primes[prime_idx] <= sval:
if sval % primes[prime_idx] == 0:
if primes[prime_idx] in factorprez:
maxsize = max(
maxsize, union(val, factorprez[primes[prime_idx]])
)
else:
factorprez[primes[prime_idx]] = val
while sval % primes[prime_idx] == 0:
sval //= primes[prime_idx]
prime_idx += 1
if sval in primeset:
if sval in factorprez:
maxsize = max(maxsize, union(val, factorprez[sval]))
else:
factorprez[sval] = val
return maxsize | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
parent = {}
def find(a):
if a not in parent:
parent[a] = a
return a
if parent[a] == a:
return a
parent[a] = find(parent[a])
return parent[a]
for num in A:
parent[num] = num
for num in A:
for i in range(2, int(sqrt(num)) + 1):
if num % i == 0:
parent[find(num)] = parent[find(i)]
parent[find(num)] = parent[find(num / i)]
count = {}
maxi = 1
for num in A:
tmp = find(num)
if tmp not in count:
count[tmp] = 1
else:
count[tmp] += 1
maxi = max(maxi, count[tmp])
return maxi
def largestComponentSize2(self, A: List[int]) -> int:
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
visited = [False] * len(A)
group = [-1] * len(A)
stack = []
num = 0
maxGrp = 0
for i in range(len(A)):
if visited[i] == False:
stack.append(i)
numGrp = 0
while len(stack) > 0:
curr = stack.pop()
for j in range(len(A)):
if j == curr:
continue
if group[j] == -1:
if A[curr] % 2 == A[j] % 2 == 0 or gcd(A[curr], A[j]) > 1:
group[j] = num
numGrp += 1
stack.append(j)
visited[i] = True
maxGrp = max(maxGrp, numGrp)
num += 1
return maxGrp | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
parent = {}
size = {}
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(rootlist):
if not rootlist:
return
for i in range(1, len(rootlist)):
parent[rootlist[i]] = rootlist[0]
size[rootlist[0]] += size[rootlist[i]]
size[rootlist[0]] += 1
fact = [[] for _ in range(len(A))]
for j in range(len(A)):
num, p = A[j], 2
while num >= p * p:
if num % p == 0:
fact[j].append(p)
size[p], parent[p] = 0, p
while num % p == 0:
num //= p
p += 1
if num > 1:
fact[j].append(num)
size[num], parent[num] = 0, num
for i in range(len(fact)):
rootset = set()
for factor in fact[i]:
rootset.add(find(factor))
union(list(rootset))
return max(size.values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR RETURN FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
d = {}
for i in range(len(A)):
for j in range(2, int(math.sqrt(A[i]) + 1)):
if A[i] % j == 0:
if j not in d:
d[j] = []
if A[i] // j not in d:
d[A[i] // j] = []
d[j].append(i)
d[A[i] // j].append(i)
if A[i] not in d:
d[A[i]] = []
d[A[i]].append(i)
self.father = list(range(len(A)))
for k, v in list(d.items()):
if len(v) > 1:
for i in range(len(v) - 1):
self.union(v[i], v[i + 1])
for i in range(len(self.father)):
self.find(i)
return Counter(self.father).most_common()[0][1]
def find(self, x):
if self.father[x] == x:
return x
else:
self.father[x] = self.find(self.father[x])
return self.father[x]
def union(self, x, y):
x_father = self.find(x)
y_father = self.find(y)
if x_father != y_father:
self.father[x_father] = y_father | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
ma = max(A)
N = len(A)
m = list(range(ma + 1))
for a in A:
for k in range(2, int(math.sqrt(a)) + 1):
if a % k == 0:
self.u(m, a, k)
self.u(m, a, a // k)
count = collections.defaultdict(int)
for a in A:
count[self.f(m, a)] += 1
return max(count.values())
def f(self, m, a):
while m[a] != a:
m[a] = m[m[a]]
a = m[a]
return a
def u(self, m, a, b):
if m[a] == m[b]:
return
pa = self.f(m, a)
pb = self.f(m, b)
m[pa] = pb | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR |
Given a non-empty array of unique positive integers A, consider the following graph:
There are A.length nodes, labelled A[0] to A[A.length - 1];
There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000 | class Solution:
def get_factors(self, n: int):
factors = set()
for factor in self.total_factors:
if factor > n:
break
if n % factor == 0:
factors.add(factor)
for factor in factors:
self.total_factors.remove(factor)
return factors
def get_prime_numbers(self, n_max: int):
self.total_factors = [2, 3]
for val in range(3, n_max // 2 + 2, 2):
for i in range(3, int(val**0.5) + 2, 2):
if val % i == 0:
break
elif i >= int(val**0.5):
self.total_factors.append(val)
def largestComponentSize(self, A: List[int]) -> int:
N = max(A) + 1
nods = {d: d for d in range(N)}
def find(x):
root = x
while nods[root] != root:
root = nods[root]
while nods[x] != root:
parent = nods[x]
nods[x] = root
x = parent
return root
def union(x, y):
rx, ry = find(x), find(y)
if rx != ry:
nods[ry] = rx
for a in A:
for factor in range(2, int(sqrt(a)) + 1):
if a % factor == 0:
union(a, factor)
union(a, a // factor)
counts = collections.defaultdict(int)
for a in A:
counts[find(a)] += 1
print(counts)
return max(counts.values())
def largestComponentSize_slow(self, A: List[int]) -> int:
A.sort()
self.get_prime_numbers(A[-1])
max_len = 0
while len(A) > max_len:
factors = self.get_factors(A[0])
new_len = 1
A.remove(A[0])
while factors:
factor = factors.pop()
to_remove = []
for i in range(len(A)):
if A[i] % factor == 0:
new_len += 1
rest = A[i] / factor
while rest % factor == 0:
rest /= factor
factors = factors | self.get_factors(rest)
to_remove.append(i)
A = [A[i] for i in range(len(A)) if i not in to_remove]
max_len = max(new_len, max_len)
return max_len | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
li = matrix.pop(0)
while len(matrix):
si = []
for i in zip(*matrix):
si.append(i)
matrix = si[::-1]
li += matrix.pop(0)
return li | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, a, m, n):
res = []
k = 0
l = 0
while k < m and l < n:
for i in range(l, n):
res.append(a[k][i])
k += 1
for i in range(k, m):
res.append(a[i][n - 1])
n -= 1
if k < m:
for i in range(n - 1, l - 1, -1):
res.append(a[m - 1][i])
m -= 1
if l < n:
for i in range(m - 1, k - 1, -1):
res.append(a[i][l])
l += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
def spirallyTraverseUtil(matrix, out, startr, endr, startc, endc):
if startr > endr or startc > endc:
return
if endr == startr:
for i in range(startc, endc + 1):
out.append(matrix[endr][i])
return
if endc == startc:
for i in range(startr, endr + 1):
out.append(matrix[i][endc])
return
if endr - startr == 1:
for i in range(startc, endc + 1):
out.append(matrix[startr][i])
for i in range(endc, startc - 1, -1):
out.append(matrix[endr][i])
return
for i in range(startc, endc + 1):
out.append(matrix[startr][i])
for i in range(startr + 1, endr + 1):
out.append(matrix[i][endc])
for i in range(endc - 1, startc - 1, -1):
out.append(matrix[endr][i])
for i in range(endr - 1, startr, -1):
out.append(matrix[i][startc])
spirallyTraverseUtil(
matrix, out, startr + 1, endr - 1, startc + 1, endc - 1
)
out = []
try:
spirallyTraverseUtil(matrix, out, 0, r - 1, 0, c - 1)
except Exception as e:
print(e)
return out | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
l = []
startc = 0
startr = 0
endc = c - 1
endr = r - 1
while startr <= endr and startc <= endc:
for i in range(startc, endc):
l.append(matrix[startr][i])
for i in range(startr, endr):
l.append(matrix[i][endc])
for i in range(endc, startc, -1):
l.append(matrix[endr][i])
for i in range(endr, startr, -1):
l.append(matrix[i][startc])
if startr == endr and startc == endc and startc == startr:
l.append(matrix[startr][endr])
startc += 1
startr += 1
endc -= 1
endr -= 1
return l[0 : r * c] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
left = 0
right = c - 1
top = 0
bottom = r - 1
ans = []
while left <= right and top <= bottom:
for i in range(left, right + 1):
ans.append(matrix[top][i])
top += 1
for i in range(top, bottom + 1):
ans.append(matrix[i][right])
right -= 1
if top <= bottom:
for i in range(right, left - 1, -1):
ans.append(matrix[bottom][i])
bottom -= 1
if left <= right:
for i in range(bottom, top - 1, -1):
ans.append(matrix[i][left])
left += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
sr, sc = 0, 0
er, ec = len(matrix) - 1, len(matrix[0]) - 1
res = []
while sr <= er and sc <= ec:
for i in range(sc, ec + 1):
res.append(matrix[sc][i])
for i in range(sr + 1, er + 1):
res.append(matrix[i][ec])
for i in range(ec - 1, sc - 1, -1):
if sr == er:
break
res.append(matrix[er][i])
for i in range(er - 1, sr, -1):
if ec == sc:
break
res.append(matrix[i][sc])
sr += 1
sc += 1
er -= 1
ec -= 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
start_row = 0
start_col = 0
end_row = r - 1
end_col = c - 1
ans = []
while len(ans) < r * c and start_row <= end_row and start_col <= end_col:
for i in range(start_col, end_col + 1):
ans.append(matrix[start_row][i])
start_row += 1
for i in range(start_row, end_row + 1):
ans.append(matrix[i][end_col])
end_col -= 1
if len(ans) < r * c and start_col <= end_col:
for i in range(end_col, start_col - 1, -1):
ans.append(matrix[end_row][i])
end_row -= 1
if len(ans) < r * c and start_row <= end_row:
for i in range(end_row, start_row - 1, -1):
ans.append(matrix[i][start_col])
start_col += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, m, r, c):
a = []
count = 0
up = 0
left = 0
right = c - 1
down = r - 1
while count < r * c:
for i in range(left, right + 1):
if count >= r * c:
break
a.append(m[up][i])
count += 1
up += 1
for i in range(up, down + 1):
if count >= r * c:
break
a.append(m[i][right])
count += 1
right -= 1
for i in range(right, left - 1, -1):
if count >= r * c:
break
a.append(m[down][i])
count += 1
down -= 1
for i in range(down, up - 1, -1):
if count >= r * c:
break
a.append(m[i][left])
count += 1
left += 1
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
top, bottom, left, right = 0, r - 1, 0, c - 1
direction = 0
result = []
while top <= bottom and left <= right:
if direction == 0:
for i in range(left, right + 1):
result.append(matrix[top][i])
top += 1
elif direction == 1:
for i in range(top, bottom + 1):
result.append(matrix[i][right])
right -= 1
elif direction == 2:
for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
bottom -= 1
elif direction == 3:
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
left += 1
direction = (direction + 1) % 4
return result | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
ans = []
rowStart, rowEnd = 0, r - 1
columnStart, columnEnd = 0, c - 1
while rowStart <= rowEnd and columnStart <= columnEnd:
for i in range(columnStart, columnEnd + 1):
ans.append(matrix[rowStart][i])
for j in range(rowStart + 1, rowEnd + 1):
ans.append(matrix[j][columnEnd])
if rowStart < rowEnd and columnStart < columnEnd:
for i in range(columnEnd - 1, columnStart, -1):
ans.append(matrix[rowEnd][i])
for j in range(rowEnd, rowStart, -1):
ans.append(matrix[j][columnStart])
rowStart += 1
rowEnd -= 1
columnStart += 1
columnEnd -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
u = 0
d = r - 1
l = 0
ri = c - 1
count = 0
i = 0
j = 0
k = 0
res = []
p = r * c - 1
while count < p:
while count < r * c and j < ri:
res.append(matrix[i][j])
count += 1
j += 1
u += 1
while count < p and i < d:
res.append(matrix[i][j])
count += 1
i += 1
ri -= 1
while count < p and j > l:
res.append(matrix[i][j])
j -= 1
count += 1
d -= 1
while count < p and i > u:
res.append(matrix[i][j])
i -= 1
count += 1
l += 1
res.append(matrix[i][j])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
current_row = 0
current_column = -1
lst = []
while r > 0 and c > 0:
count = 0
while count < c:
current_column += 1
count += 1
lst.append(matrix[current_row][current_column])
count = 1
row_changed = False
while count < r:
current_row += 1
count += 1
lst.append(matrix[current_row][current_column])
row_changed = True
count = 1
column_changed = False
while row_changed and count < c:
current_column -= 1
count += 1
lst.append(matrix[current_row][current_column])
column_changed = True
count = 2
while column_changed and count < r:
current_row -= 1
count += 1
lst.append(matrix[current_row][current_column])
r = r - 2
c = c - 2
return lst | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
ROWS = list(range(r))
COLS = list(range(c))
res = []
while ROWS and COLS:
rowStart = ROWS.pop(0)
for col in COLS:
res.append(matrix[rowStart][col])
colEnd = COLS.pop()
for row in ROWS:
res.append(matrix[row][colEnd])
if ROWS:
rowEnd = ROWS.pop()
for col in COLS[::-1]:
res.append(matrix[rowEnd][col])
if COLS:
colStart = COLS.pop(0)
for row in ROWS[::-1]:
res.append(matrix[row][colStart])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
res = []
while matrix:
res += matrix.pop(0)
matrix = list(zip(*matrix))[::-1]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
res = []
if len(matrix) == 0:
return []
m = len(matrix)
di = 0
row = [0, 1, 0, -1]
col = [1, 0, -1, 0]
x = 0
y = 0
seen = [[(0) for i in range(c)] for j in range(m)]
for i in range(m * c):
res.append(matrix[x][y])
seen[x][y] = True
cr = x + row[di]
cc = y + col[di]
if 0 <= cr and cr < m and 0 <= cc and cc < c and not seen[cr][cc]:
x = cr
y = cc
else:
di = (di + 1) % 4
x = x + row[di]
y = y + col[di]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
ans = []
if len(matrix) == 0:
return ans
m = len(matrix)
n = len(matrix[0])
seen = [[(0) for i in range(n)] for j in range(m)]
dr = [0, 1, 0, -1]
dc = [1, 0, -1, 0]
x = 0
y = 0
di = 0
for i in range(m * n):
ans.append(matrix[x][y])
seen[x][y] = True
cr = x + dr[di]
cc = y + dc[di]
if 0 <= cr and cr < m and 0 <= cc and cc < n and not seen[cr][cc]:
x = cr
y = cc
else:
di = (di + 1) % 4
x += dr[di]
y += dc[di]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
x1, y1 = 0, 0
x2, y2 = r - 1, c - 1
ans = []
i, j = 0, 0
p = 0
while p < r * c:
if i == x1 and j != y2:
ans.append(matrix[i][j])
j += 1
elif j == y2 and i != x2:
ans.append(matrix[i][j])
i += 1
elif i == x2 and j != y1:
ans.append(matrix[i][j])
j -= 1
elif j == y1 and i != x1:
ans.append(matrix[i][j])
i -= 1
if i == x1:
x1 += 1
y1 += 1
x2 -= 1
y2 -= 1
i, j = x1, y1
if x1 == x2 and y1 == y2:
ans.append(matrix[i][j])
p += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
n = r * c
d = 1
p = 0 + 0.0j
x0, x1 = 0, c - 1
y0, y1 = 0, r - 1
ans = []
while n > 0:
y, x = int(p.imag), int(p.real)
ans.append(matrix[y][x])
if d == 1 + 0.0j and x == x1:
y0 += 1
d *= 1.0j
if d == 1.0j and y == y1:
x1 -= 1
d *= 1.0j
if d == -1 + 0.0j and x == x0:
y1 -= 1
d *= 1.0j
if d == -1.0j and y == y0:
x0 += 1
d *= 1.0j
p += d
n -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
top = 0
down = r
left = 0
right = c
d = 0
l = []
while top < down and left < right:
if d == 0:
for i in range(left, right):
l.append(matrix[top][i])
top += 1
if d == 1:
for i in range(top, down):
l.append(matrix[i][right - 1])
right -= 1
if d == 2:
for i in range(right - 1, left - 1, -1):
l.append(matrix[down - 1][i])
down -= 1
if d == 3:
for i in range(down - 1, top - 1, -1):
l.append(matrix[i][left])
left += 1
d = (d + 1) % 4
return l | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
flattened_matrix = []
while matrix:
try:
r1 = matrix.pop(0)
for i in r1:
flattened_matrix.append(i)
except:
pass
try:
for j in matrix:
val = j.pop()
flattened_matrix.append(val)
except:
pass
try:
r_last = matrix.pop()[::-1]
for k in r_last:
flattened_matrix.append(k)
except:
pass
try:
left_side = []
for l in matrix:
val = l.pop(0)
left_side.append(val)
flattened_matrix += left_side[::-1]
except:
pass
return flattened_matrix | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
start_row_ind = 0
start_col_ind = 0
end_row_ind = r - 1
end_col_ind = c - 1
count = 0
total_elements = r * c
res = []
while count < total_elements:
i = start_col_ind
while i <= end_col_ind and count < total_elements:
res.append(matrix[start_row_ind][i])
i += 1
count += 1
start_row_ind += 1
i = start_row_ind
while i <= end_row_ind and count < total_elements:
res.append(matrix[i][end_col_ind])
i += 1
count += 1
end_col_ind -= 1
i = end_col_ind
while i >= start_col_ind and count < total_elements:
res.append(matrix[end_row_ind][i])
i -= 1
count += 1
end_row_ind -= 1
i = end_row_ind
while i >= start_row_ind and count < total_elements:
res.append(matrix[i][start_col_ind])
i -= 1
count += 1
start_col_ind += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
ans = []
top = -1
right = c
left = -1
bottom = r
i = 0
j = 0
while top < bottom and left < right:
while j < right:
ans.append(matrix[i][j])
j += 1
j -= 1
top += 1
i += 1
if left + 1 == right or top + 1 == bottom:
break
while i < bottom:
ans.append(matrix[i][j])
i += 1
i -= 1
right -= 1
j -= 1
if left + 1 == right or top + 1 == bottom:
break
while j > left:
ans.append(matrix[i][j])
j -= 1
j += 1
bottom -= 1
i -= 1
if left + 1 == right or top + 1 == bottom:
break
while i > top:
ans.append(matrix[i][j])
i -= 1
i += 1
left += 1
j += 1
if left + 1 == right or top + 1 == bottom:
break
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR |
Given a matrix of size r*c. Traverse the matrix in spiral form.
Example 1:
Input:
r = 4, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation:
Example 2:
Input:
r = 3, c = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
Output:
1 2 3 4 8 12 11 10 9 5 6 7
Explanation:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.
Your Task:
You dont need to read input or print anything. Complete the function spirallyTraverse() that takes matrix, r and c as input parameters and returns a list of integers denoting the spiral traversal of matrix.
Expected Time Complexity: O(r*c)
Expected Auxiliary Space: O(r*c), for returning the answer only.
Constraints:
1 <= r, c <= 100
0 <= matrix_{i} <= 100 | class Solution:
def spirallyTraverse(self, matrix, r, c):
i = r
j = c
r = 0
c = 0
ans = []
direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]
reverse = [[1, -1], [-1, -1], [-1, 1], [1, 1]]
order = 0
def outOfBound(r, c):
return r < 0 or c < 0 or r == i or c == j
while True:
if outOfBound(r, c) or matrix[r][c] == -9999:
r += reverse[order][0]
c += reverse[order][1]
order = (order + 1) % 4
if len(ans) == i * j:
break
ans.append(matrix[r][c])
matrix[r][c] = -9999
r += direction[order][0]
c += direction[order][1]
len(ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Shashank loves trees and math. He has a rooted tree, $\mathbf{T}$, consisting of $N$ nodes uniquely labeled with integers in the inclusive range $[1,N]$. The node labeled as $1$ is the root node of tree $\mathbf{T}$, and each node in $\mathbf{T}$ is associated with some positive integer value (all values are initially $\mbox{0}$).
Let's define $\mathbf{F_k}$ as the $k^{th}$ Fibonacci number. Shashank wants to perform $2$ types of operations over his tree, $\mathbf{T}$:
$\mbox{U}$ $\mbox{X}$ $\boldsymbol{\mbox{k}}$
Update the subtree rooted at node $\mbox{X}$ such that the node at level $0$ in subtree $\mbox{X}$ (i.e., node $\mbox{X}$) will have $\mathbf{F_k}$ added to it, all the nodes at level $\mbox{I}$ will have $\boldsymbol{F}_{k+1}$ added to them, and so on. More formally, all the nodes at a distance $\mbox{D}$ from node $\mbox{X}$ in the subtree of node $\mbox{X}$ will have the $(k+D)^{\mathrm{th}}$ Fibonacci number added to them.
$Q$ $\mbox{X}$ $\mathbf{Y}$
Find the sum of all values associated with the nodes on the unique path from $\mbox{X}$ to $\mathbf{Y}$. Print your sum modulo $10^9+7$ on a new line.
Given the configuration for tree $\mathbf{T}$ and a list of $\mbox{M}$ operations, perform all the operations efficiently.
Note: $F_1=F_2=1$.
Input Format
The first line contains $2$ space-separated integers, $N$ (the number of nodes in tree $\mathbf{T}$) and $\mbox{M}$ (the number of operations to be processed), respectively.
Each line $\boldsymbol{i}$ of the $N-1$ subsequent lines contains an integer, $\mbox{P}$, denoting the parent of the $(i+1)^{th}$ node.
Each of the $\mbox{M}$ subsequent lines contains one of the two types of operations mentioned in the Problem Statement above.
Constraints
$1\leq N,M\leq10^5$
$1\leq X,Y\leq N$
$1\leq k\leq10^{15}$
Output Format
For each operation of type $2$ (i.e., $Q$), print the required answer modulo $10^9+7$ on a new line.
Sample Input
5 10
1
1
2
2
Q 1 5
U 1 1
Q 1 1
Q 1 2
Q 1 3
Q 1 4
Q 1 5
U 2 2
Q 2 3
Q 4 5
Sample Output
0
1
2
2
4
4
4
10
Explanation
Intially, the tree looks like this:
After update operation $1\:\:1$, it looks like this:
After update operation $\textbf{2}\:\textbf{2}$, it looks like this: | mod = 10**9 + 7
cache = {(0): (0, 1), (1): (1, 1)}
def fib_pair(n):
if n in cache:
return cache[n]
hn = n // 2
f, fp = fib_pair(hn)
if n & 1:
res = (fp * fp + f * f) % mod, (2 * f + fp) * fp % mod
else:
fm = fp - f
if fm < 0:
fm += mod
res = (2 * fm + f) * f % mod, (fp * fp + f * f) % mod
if n < 1000000:
cache[n] = res
return res
n, m = map(int, input().strip().split())
parents = (n + 1) * [0]
children = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
j = int(input())
parents[i] = j
children[j].append(i)
depths = (n + 1) * [0]
stack = [(1, 1)]
while stack:
i, d = stack.pop()
depths[i] = d
for j in children[i]:
stack.append((j, d + 1))
nrs = (n + 1) * [0]
for _ in range(m):
q, si, sj = input().strip().split()
if q == "U":
x, k = int(si), int(sj)
fibs = list(fib_pair(k))
stack = [(x, 0)]
while stack:
y, l = stack.pop()
if l >= len(fibs):
fibs.append((fibs[-1] + fibs[-2]) % mod)
nrs[y] += fibs[l]
for z in children[y]:
stack.append((z, l + 1))
else:
i, j = int(si), int(sj)
if depths[i] < depths[j]:
i, j = j, i
fsum = 0
while depths[j] < depths[i]:
fsum += nrs[i]
i = parents[i]
while i != j:
fsum += nrs[i] + nrs[j]
j = parents[j]
i = parents[i]
fsum += nrs[i]
print(fsum % mod) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
Note:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20 | class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
def encodeWord(word):
d, nextChar, i, encoding = (
{},
iter([chr(ord("a") + i) for i in range(26)]),
0,
[],
)
while i < len(word):
c, count = word[i], 1
while i + 1 < len(word) and word[i + 1] == c:
i, count = i + 1, count + 1
if c not in d:
d[c] = next(nextChar)
encoding.append(d[c] + str(count))
i += 1
return "".join(encoding)
p_enc = encodeWord(pattern)
return [word for word in words if encodeWord(word) == p_enc] | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR DICT FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER NUMBER LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR |
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
Note:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20 | class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
out = []
for word in words:
pat_dict = dict()
used = set()
if len(word) == len(pattern):
can_be = True
for i in range(len(word)):
if word[i] not in pat_dict:
if pattern[i] not in used:
pat_dict[word[i]] = pattern[i]
used.add(pattern[i])
else:
can_be = False
break
elif pat_dict[word[i]] != pattern[i]:
can_be = False
break
if can_be == True:
out.append(word)
return out | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
Note:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20 | class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
plen = len(pattern)
pat = [-1] * plen
pat[0] = 0
for i in range(1, plen):
for j in range(i):
if pattern[j] == pattern[i]:
pat[i] = j
res = []
for word in words:
wlen = len(word)
flag = 0
if wlen != plen:
continue
for i in range(1, wlen):
if pat[i] != -1 and word[i] == word[pat[i]]:
continue
elif pat[i] != -1 and word[i] != word[pat[i]]:
flag = 1
break
else:
for j in range(i):
if word[j] == word[i]:
flag = 1
break
if flag == 0:
res.append(word)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
Note:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20 | class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
def match(list1, pattern):
for i in range(len(pattern)):
if list1[i] != pattern[i]:
return False
return True
def number_arr(list1):
dict = {}
arr = []
last_given_value = 0
for i in list1:
if i not in dict:
last_given_value += 1
dict[i] = last_given_value
arr.append(dict[i])
else:
arr.append(dict[i])
return arr
pat = number_arr(pattern)
ans = []
for word in words:
word_list = number_arr(word)
if match(pat, word_list):
ans.append(word)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
Note:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20 | class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
ans = []
for i in words:
temp = i
if len(pattern) == len(temp):
as1 = {}
got = []
found = 1
for i1 in range(len(temp)):
if pattern[i1] not in as1:
if temp[i1] in got:
found = 0
break
else:
as1[pattern[i1]] = temp[i1]
got.append(temp[i1])
elif temp[i1] != as1.get(pattern[i1]):
found = 0
break
if found == 1:
ans.append(temp)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
Note:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20 | class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
output = []
for word in words:
fwd, reverse, valid = defaultdict(str), defaultdict(str), True
for i in range(len(word)):
if word[i] not in fwd and pattern[i] not in reverse:
fwd[word[i]] = pattern[i]
reverse[pattern[i]] = word[i]
else:
valid = (
fwd[word[i]] == pattern[i] and reverse[pattern[i]] == word[i]
)
if not valid:
break
if valid:
output.append(word)
return output | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
l = []
r = []
for i in a:
if i < 0:
l.append(i)
else:
r.append(i)
x = len(l)
for i in range(n):
if i < x:
a[i] = l[i]
else:
a[i] = r[i - x] | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(arr, n):
current_index = 0
ar = []
for i in range(n):
if arr[i] < 0:
p = arr[i]
arr[i] = arr[current_index]
arr[current_index] = p
current_index += 1
else:
ar.append(arr[i])
count = 0
for j in range(current_index, n):
arr[j] = ar[count]
count += 1
return arr | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def merge(arr, l, m, r):
i, j, k = 0, 0, 0
n1 = m - l + 1
n2 = r - m
L = [arr[l + i] for i in range(n1)]
R = [arr[m + 1 + j] for j in range(n2)]
i = 0
j = 0
k = l
while i < n1 and L[i] < 0:
arr[k] = L[i]
k += 1
i += 1
while j < n2 and R[j] < 0:
arr[k] = R[j]
k += 1
j += 1
while i < n1:
arr[k] = L[i]
k += 1
i += 1
while j < n2:
arr[k] = R[j]
k += 1
j += 1
def RearrangePosNeg(arr, l, r):
if l < r:
m = l + (r - l) // 2
RearrangePosNeg(arr, l, m)
RearrangePosNeg(arr, m + 1, r)
merge(arr, l, m, r)
def Rearrange(arr, n):
RearrangePosNeg(arr, 0, n - 1) | FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
x = []
x[:] = a[:]
a.clear()
index = 0
for i in range(len(x)):
if x[i] < 0:
a.insert(index, x[i])
index += 1
else:
a.append(x[i]) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
neg = 0
for i in range(n):
if a[i] < 0:
c = a.pop(i)
a.insert(neg, c)
neg += 1
return a | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
negative = []
positive = []
for i in a:
if i < 0:
negative.append(i)
else:
positive.append(i)
a[:] = negative + positive | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
list1 = []
list2 = []
for i in a:
if i < 0:
list1.append(i)
else:
list2.append(i)
ans = list1 + list2
for i in range(n):
a[i] = ans[i]
return a | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(arr, n):
negative = []
positive = []
for i in range(n):
if arr[i] < 0:
negative.append(arr[i])
else:
positive.append(arr[i])
negative.extend(positive)
arr[:] = negative[:] | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
l1 = []
for i in a:
if i < 0:
l1.append(i)
for i in a:
if i >= 0:
l1.append(i)
for i in range(n):
a[i] = l1[i] | FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(arr, N):
p = []
n = []
for i in range(N):
if arr[i] >= 0:
p.append(arr[i])
else:
n.append(arr[i])
for i in range(len(n)):
arr[i] = n[i]
for i in range(len(p)):
arr[i + len(n)] = p[i]
return arr | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
b = []
c = []
i = 0
while i < n:
if a[i] >= 0:
b.append(a[i])
else:
c.append(a[i])
i += 1
j = 0
for i in c:
a[j] = i
j += 1
for i in b:
a[j] = i
j += 1 | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
l = []
l.extend(a)
k = []
z = []
a.clear()
for i in l:
if i < 0:
z.append(i)
else:
k.append(i)
z.extend(k)
a.extend(z) | FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
po = []
ne = []
for i in range(n):
if a[i] < 0:
ne.append(a[i])
else:
po.append(a[i])
for i in range(n):
if i < len(ne):
a[i] = ne[i]
else:
a[i] = po[i - len(ne)] | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR |
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-negative numbers.
(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 2:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{5}
-10^{9} ≤ Elements of array ≤ 10^{9} | def Rearrange(a, n):
l = []
x = []
for i in a:
if i < 0:
l.append(i)
else:
x.append(i)
b = []
b.extend(l)
b.extend(x)
for i in range(0, len(b)):
a[i] = b[i]
return a | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.