description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
col_sum = [([0] * N) for i in range(N)]
for j in range(N):
for i in range(K):
col_sum[i][j] = mat[i][j] + (col_sum[i - 1][j] if i > 0 else 0)
for j in range(N):
for i in range(K, N):
col_sum[i][j] = mat[i][j] + col_sum[i - 1][j] - mat[i - K][j]
max_sum = float("-inf")
for i in range(K - 1, N):
grid_sum = 0
for j in range(K - 1):
grid_sum += col_sum[i][j]
for j in range(K - 1, N):
grid_sum += col_sum[i][j]
if j >= K:
grid_sum -= col_sum[i][j - K]
max_sum = max(max_sum, grid_sum)
return max_sum | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
dp = [([0] * (N + 1)) for i in range(N + 1)]
for i in range(1, N + 1):
for j in range(1, N + 1):
if i == 1:
dp[i][j] = dp[i][j - 1] + mat[i - 1][j - 1]
elif j == 1:
dp[i][j] = dp[i - 1][j] + mat[i - 1][j - 1]
else:
dp[i][j] = (
dp[i - 1][j]
+ dp[i][j - 1]
+ mat[i - 1][j - 1]
- dp[i - 1][j - 1]
)
ma = 0
for i in range(k, N + 1):
for j in range(k, N + 1):
ma = max(ma, dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k])
return ma | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
prefixsum = [[(0) for i in range(N)] for m in mat]
prefixsum[0][0] = mat[0][0]
for r in range(N):
currentrow = 0
for c in range(N):
currentrow += mat[r][c]
prefixsum[r][c] = currentrow + (prefixsum[r - 1][c] if r > 0 else 0)
def sumBlock(i, j, pre):
if i + K <= N and j + K <= N:
A = pre[i + K - 1][j + K - 1]
B = pre[i - 1][j + K - 1] if i > 0 else 0
C = pre[i + K - 1][j - 1] if j > 0 else 0
D = pre[i - 1][j - 1] if i > 0 and j > 0 else 0
return A - B - C + D
else:
return -(2**32)
bestsub = -(2**32)
for r in range(N - K + 1):
for c in range(N - K + 1):
block = sumBlock(r, c, prefixsum)
bestsub = max(bestsub, block)
return bestsub | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | import sys
class Solution:
def Maximum_Sum(self, mat, N, K):
dp = [([0] * (N + 1)) for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(1, N + 1):
dp[i][j] = (
mat[i - 1][j - 1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
)
max_sum = -sys.maxsize
for i in range(K, N + 1):
for j in range(K, N + 1):
sum = dp[i][j]
if i - K >= 0:
sum -= dp[i - K][j]
if j - K >= 0:
sum -= dp[i][j - K]
if i - K >= 0 and j - K >= 0:
sum += dp[i - K][j - K]
max_sum = max(max_sum, sum)
return max_sum | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, n, k):
dp = [[(-1000000) for i in range(n)] for j in range(n)]
sm = 0
for i in range(n):
sm = 0
for j in range(n):
if j < k:
sm = sm + mat[i][j]
if j == k - 1:
dp[i][j] = sm
else:
sm = sm + mat[i][j] - mat[i][j - k]
dp[i][j] = sm
dp1 = [[(-1000000) for i in range(n)] for j in range(n)]
for i in range(n):
sm = 0
for j in range(n):
if j < k:
sm = sm + dp[j][i]
if j == k - 1:
dp1[j][i] = sm
else:
sm = sm + dp[j][i] - dp[j - k][i]
dp1[j][i] = sm
mx = -1000000
for i in range(n):
for j in range(n):
mx = max(mx, dp1[i][j])
return mx | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
ps = [[(0) for _ in range(N)] for _ in range(N)]
s = s2 = 0
for i in range(N):
s += mat[i][0]
s2 += mat[0][i]
ps[i][0] = s
ps[0][i] = s2
for i in range(1, N):
for j in range(1, N):
ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + mat[i][j]
maxi = -float("inf")
for i in range(K - 1, N):
for j in range(K - 1, N):
x = y = z = 0
if i - K >= 0:
x = ps[i - K][j]
if j - K >= 0:
y = ps[i][j - K]
if i - K >= 0 and j - K >= 0:
z = ps[i - K][j - K]
s = ps[i][j] - x - y + z
maxi = max(maxi, s)
return maxi | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
temp = [[(-1) for i in range(0, N)] for i in range(0, N)]
for ind in range(0, N):
i, j = 0, 0
summ = 0
while j < n:
summ += mat[ind][j]
if j - i + 1 == K:
temp[ind][j] = summ
summ -= mat[ind][i]
i += 1
j += 1
maxx = -float("inf")
for ind in range(0, N):
i, j = 0, 0
summ = 0
while j < n:
summ += temp[j][ind]
if j - i + 1 == K:
maxx = max(maxx, summ)
summ -= temp[i][ind]
i += 1
j += 1
return maxx | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
arr = mat
n = N
k = K
dp = [([0] * n) for i in range(n)]
for i in range(n):
for j in range(n):
if i == 0:
dp[i][j] = arr[i][j]
elif i - k >= 0:
dp[i][j] = dp[i - 1][j] + arr[i][j] - arr[i - k][j]
else:
dp[i][j] = dp[i - 1][j] + arr[i][j]
ans = [([0] * n) for i in range(n)]
maxval = -10000000
for i in range(k - 1, n):
for j in range(n):
if j == 0:
ans[i][j] = dp[i][j]
elif j - k >= 0:
ans[i][j] = dp[i][j] + ans[i][j - 1] - dp[i][j - k]
else:
ans[i][j] = dp[i][j] + ans[i][j - 1]
if j - k >= -1:
maxval = max(maxval, ans[i][j])
return maxval
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
matrix = []
for _ in range(n):
matrix.append([int(x) for x in input().strip().split()])
k = int(input())
obj = Solution()
print(obj.Maximum_Sum(matrix, n, k)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
start = 0
for i in range(K):
for j in range(K):
start += mat[i][j]
dp = [[(0) for j in range(N)] for i in range(N)]
dp[K - 1][K - 1] = start
rowSum = [[y for y in x] for x in mat]
colSum = [[y for y in x] for x in mat]
for i in range(N):
for j in range(N):
if j > 0:
rowSum[i][j] += rowSum[i][j - 1]
if i > 0:
colSum[i][j] += colSum[i - 1][j]
res = start
for i in range(K - 1, N):
for j in range(K - 1, N):
if j == K - 1:
if i == K - 1:
continue
prev = dp[i - 1][j]
addRow = rowSum[i][j]
removeRow = rowSum[i - K][j]
dp[i][j] = prev + addRow - removeRow
res = max(res, dp[i][j])
continue
prev = dp[i][j - 1]
addCol = colSum[i][j] - (colSum[i - K][j] if i - K >= 0 else 0)
removeCol = colSum[i][j - K] - (
colSum[i - K][j - K] if i - K >= 0 else 0
)
dp[i][j] = prev + addCol - removeCol
res = max(res, dp[i][j])
return res
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
matrix = []
for _ in range(n):
matrix.append([int(x) for x in input().strip().split()])
k = int(input())
obj = Solution()
print(obj.Maximum_Sum(matrix, n, k)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
prefix = [[(0) for i in range(N)] for j in range(N)]
prefix[0][0] = mat[0][0]
for i in range(1, N):
prefix[0][i] += prefix[0][i - 1] + mat[0][i]
prefix[i][0] += mat[i][0] + prefix[i - 1][0]
m = -1
for i in range(1, N):
for j in range(1, N):
prefix[i][j] = (
prefix[i][j - 1]
+ (prefix[i - 1][j] - prefix[i - 1][j - 1])
+ mat[i][j]
)
m = float("-inf")
for i in range(N):
for j in range(N):
mat[i][j] = prefix[i][j]
for i in range(K - 1, N):
for j in range(K - 1, N):
if i > K - 1:
prefix[i][j] -= mat[i - K][j]
if j > K - 1:
prefix[i][j] -= mat[i][j - K]
if i > K - 1 and j > K - 1:
prefix[i][j] += mat[i - K][j - K]
if prefix[i][j] > m:
m = prefix[i][j]
return m | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | import sys
class Solution:
def getSum(self, mat, n):
for i in range(n):
for j in range(n):
top, left, daigonal = 0, 0, 0
if i - 1 >= 0:
top = mat[i - 1][j]
if j - 1 >= 0:
left = mat[i][j - 1]
if i - 1 >= 0 and j - 1 >= 0:
daigonal = mat[i - 1][j - 1]
mat[i][j] = top + left - daigonal + mat[i][j]
def Maximum_Sum(self, mat, N, K):
maxSum = [-sys.maxsize]
self.getSum(mat, N)
for i in range(N - K + 1):
for j in range(N - K + 1):
end_row = i + K - 1
end_col = j + K - 1
top, left, daigonal = 0, 0, 0
if i - 1 >= 0:
top = mat[i - 1][end_col]
if j - 1 >= 0:
left = mat[end_row][j - 1]
if i - 1 >= 0 and j - 1 >= 0:
daigonal = mat[i - 1][j - 1]
s = mat[end_row][end_col]
s = s - top - left + daigonal
maxSum[0] = max(s, maxSum[0])
return maxSum[0] | IMPORT CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
for i in range(1, N):
mat[0][i] += mat[0][i - 1]
mat[i][0] += mat[i - 1][0]
for i in range(1, N):
for j in range(1, N):
mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1]
maxi = mat[K - 1][K - 1]
for i in range(K, N):
maxi = max(maxi, mat[i][K - 1] - mat[i - K][K - 1])
maxi = max(maxi, mat[K - 1][i] - mat[K - 1][i - K])
for i in range(K, N):
for j in range(K, N):
maxi = max(
maxi, mat[i][j] + mat[i - K][j - K] - mat[i][j - K] - mat[i - K][j]
)
return maxi | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
pref = mat.copy()
for i in range(1, N):
pref[0][i] += pref[0][i - 1]
pref[i][0] += pref[i - 1][0]
for i in range(1, N):
for j in range(1, N):
pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1]
mx = -999999999999999
for i in range(K - 1, N):
for j in range(K - 1, N):
k = pref[i][j]
if i - K >= 0:
k -= pref[i - K][j]
if j - K >= 0:
k -= pref[i][j - K]
if i - K >= 0 and j - K >= 0:
k += pref[i - K][j - K]
mx = max(mx, k)
return mx | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
for i in range(1, N):
for j in range(N):
mat[i][j] += mat[i - 1][j]
ans = -(2**63) + 1
dp = [([0] * N) for i in range(N)]
start = 0
for i in range(K):
for j in range(N):
dp[i][j] = mat[i][j]
for i in range(K, N):
for j in range(N):
dp[i][j] = mat[i][j] - mat[start][j]
start += 1
for i in range(K - 1, N):
for j in range(N - K + 1):
ans = max(ans, sum(dp[i][j : j + K]))
return ans | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, K):
for i in range(N):
for j in range(1, N):
mat[i][j] += mat[i][j - 1]
for i in range(1, N):
for j in range(N):
mat[i][j] += mat[i - 1][j]
ans = 0
for i in range(K - 1, N):
for j in range(K - 1, N):
cur = mat[i][j]
cur -= mat[i - K][j] if i - K >= 0 else 0
cur -= mat[i][j - K] if j - K >= 0 else 0
cur += mat[i - K][j - K] if min(i - K, j - K) >= 0 else 0
ans = max(ans, cur)
return ans | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K.
Note: Coins of the negative denomination are also possible at Geekland.
Example 1:
Input: N = 5, K = 3
mat[[]] = {1, 1, 1, 1, 1}
{2, 2, 2, 2, 2}
{3, 8, 6, 7, 3}
{4, 4, 4, 4, 4}
{5, 5, 5, 5, 5}
Output: 48
Explanation: {8, 6, 7}
{4, 4, 4}
{5, 5, 5}
has the maximum sum
Example 2:
Input: N = 1, K = 1
mat[[]] = {{4}}
Output: 4
Your Task:
You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ K ≤ N ≤ 10^{3}
-5*10^{5} ≤ mat[i][j] ≤ 5*10^{5} | class Solution:
def Maximum_Sum(self, mat, N, k):
dp = [[(0) for _ in range(N)] for _ in range(N)]
for i in range(N):
for j in range(N):
if i == 0 and j == 0:
dp[i][j] = mat[i][j]
elif i == 0:
dp[i][j] = mat[i][j] + dp[i][j - 1]
elif j == 0:
dp[i][j] = mat[i][j] + dp[i - 1][j]
else:
dp[i][j] = (
mat[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
)
result = float("-inf")
for i in range(k - 1, N):
for j in range(k - 1, N):
if i == k - 1 and j == k - 1:
result = max(result, dp[i][j])
elif i == k - 1:
result = max(result, dp[i][j] - dp[i][j - k])
elif j == k - 1:
result = max(result, dp[i][j] - dp[i - k][j])
else:
result = max(
result,
dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k],
)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1 | def update(bit, i):
while i < len(bit):
bit[i] += 1
i += i & -i
def getsum(bit, i):
res = 0
while i > 0:
res += bit[i]
i -= i & -i
return res
n = int(input())
arr = list(map(int, input().split()))
l = sorted(arr)
d = {}
ind = 1
for i in range(n):
if not d.get(l[i], 0):
d[l[i]] = ind
ind += 1
for i in range(n):
arr[i] = d[arr[i]]
small = [0] * n
maxel = max(arr)
bit = [0] * (maxel + 1)
for i in range(n):
c = getsum(bit, arr[i] - 1)
small[i] = i - c
update(bit, arr[i])
bit = [0] * (maxel + 1)
large = [0] * n
for i in range(n - 1, -1, -1):
k = getsum(bit, arr[i] - 1)
update(bit, arr[i])
large[i] = k
count = 0
for i in range(n):
count += small[i] * large[i]
print(count) | FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1 | def and_i(i):
return i & i + 1
def or_i(i):
return i | i + 1
class Tree:
def __init__(self, n):
self.n = n
self.tree = [0] * n
def get_sum(self, i):
ans = 0
while i >= 0:
ans += self.tree[i]
i = and_i(i) - 1
return ans
def update_tree(self, i, x=1):
while i < self.n:
self.tree[i] += x
i = or_i(i)
n = int(input())
heights = list(map(int, input().split()))
pos = {x: i for i, x in enumerate(heights)}
for ind, x in enumerate(sorted(heights, reverse=True)):
heights[pos[x]] = ind
tree1 = Tree(n)
tree2 = Tree(n)
count = 0
for h in heights:
count += tree2.get_sum(h - 1)
tree1.update_tree(h)
v = tree1.get_sum(h - 1)
tree2.update_tree(h, v)
print(count) | FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1 | from sys import stdin, stdout
def binarysearch(x, first, last, b):
mid = (first + last) / 2
mid = int(mid)
if b[first] == x:
return first
if b[last] == x:
return last
if b[mid] == x:
return mid
if b[mid] < x:
return binarysearch(x, mid, last, b)
return binarysearch(x, first, mid, b)
n = int(stdin.readline())
a = [0]
a.extend(list(map(int, stdin.readline().strip().split())))
b = []
for i in range(1, n + 1):
b.append(a[i])
b.sort()
for i in range(1, n + 1):
a[i] = binarysearch(a[i], 0, n - 1, b) + 1
fen1 = []
for i in range(0, n + 1):
fen1.append(0)
def update1(it, val):
while it <= n:
fen1[it] += val
it += it & -it
def getsum1(l, r):
if r < l:
return 0
s = 0
l -= 1
while r > 0:
s += fen1[r]
r -= r & -r
while l > 0:
s -= fen1[l]
l -= l & -l
return s
ans = 0
cnt = []
for i in range(0, n + 1):
cnt.append(0)
for i in range(1, n + 1):
update1(a[i], 1)
cnt[i] = getsum1(a[i] + 1, n)
for i in range(0, n + 1):
fen1[i] = 0
for i in range(1, n + 1):
update1(a[i], cnt[i])
ans += getsum1(a[i] + 1, n)
print(ans) | FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1 | from sys import stdin, stdout
def getSum(BiTree, i, n):
sum = 0
while i > 0:
sum += BiTree[i]
i -= i & -i
return sum
def update(BiTree, i, n, val):
while i < n:
BiTree[i] += val
i += i & -i
def construct(a, BiTree, a2, a3, n):
lcount = [(0) for i in range(n)]
for i in range(0, n):
lcount[i] = getSum(BiTree, a3[i], n)
update(BiTree, a3[i] + 1, n, 1)
rcount = [(0) for i in range(n)]
BiTree = [(0) for i in range(0, n + 1)]
for i in range(n - 1, -1, -1):
rcount[i] = getSum(BiTree, a2[i], n)
update(BiTree, a2[i] + 1, n, 1)
s = 0
for i in range(0, n):
s += rcount[i] * lcount[i]
return s
n = int(stdin.readline())
a = [int(i) for i in stdin.readline().split()]
BiTree = [(0) for i in range(0, n + 1)]
temp = sorted(a)
temp2 = temp[::-1]
d = {temp[i]: i for i in range(0, n)}
a2 = [d[a[i]] for i in range(0, n)]
d2 = {temp2[i]: i for i in range(0, n)}
a3 = [d2[a[i]] for i in range(0, n)]
stdout.write(str(construct(a, BiTree, a2, a3, n))) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1 | class Tree:
def __init__(self, n):
self.n = n
self.tree = [0] * n
def fenwick(self, i):
return i & i + 1
def get(self, r):
ans = 0
while r >= 0:
ans += self.tree[r]
r = self.fenwick(r) - 1
return ans
def get2(self, l, r):
return self.get(r) - self.get(l - 1)
def upd(self, i):
return i | i + 1
def add(self, i, x=1):
while i < self.n:
self.tree[i] += x
i = self.upd(i)
n = int(input())
a = list(map(int, input().split()))
pos = {x: i for i, x in enumerate(a)}
for i, x in enumerate(sorted(a, reverse=True)):
a[pos[x]] = i
tree1 = Tree(n)
tree2 = Tree(n)
cnt = 0
for i in a:
cnt += tree2.get(i - 1)
tree1.add(i, 1)
v = tree1.get(i - 1)
tree2.add(i, v)
print(cnt) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are.
Input
The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1 | def update(bit, i):
while i < len(bit):
bit[i] += 1
i += i & -i
def getsum(bit, i):
res = 0
while i > 0:
res += bit[i]
i -= i & -i
return res
n = int(input())
arr = list(map(int, input().split()))
d = {}
l = list(sorted(set(arr)))
ind = 1
for i in range(len(l)):
d[l[i]] = ind
ind += 1
l = [0] * n
r = [0] * n
t = 0
bit = [0] * (ind + 10)
update(bit, d[arr[0]])
for i in range(1, n):
l[i] = getsum(bit, ind + 1) - getsum(bit, d[arr[i]])
update(bit, d[arr[i]])
bit = [0] * (ind + 10)
for i in range(n - 1, -1, -1):
r[i] = getsum(bit, d[arr[i]] - 1)
update(bit, d[arr[i]])
ans = 0
for i in range(n):
ans += l[i] * r[i]
print(ans) | FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def mirrorTree(self, root1, root2):
if root1 is None:
return
if root1.data == target:
self.mirrorNode = root2.data
if root1.right:
root2.left = Node(root1.right.data)
self.mirrorTree(root1.right, root2.left)
if root1.left:
root2.right = Node(root1.left.data)
self.mirrorTree(root1.left, root2.right)
def helper(self, root1, root2, target):
if root1 is None or root2 is None:
return
if root1.data == target and root2:
self.mirrorNode = root2.data
self.helper(root1.left, root2.left, target)
self.helper(root1.right, root2.right, target)
def findMirror(self, root, target):
if root.data == target:
return root.data
root1 = Node(root.data)
self.mirrorTree(root, root1)
self.mirrorNode = -1
self.helper(root, root1, target)
return self.mirrorNode
class Solution:
def helper(self, root1, root2, target):
if root1 is None or root2 is None:
return
if root1.data == target and root2:
self.mirrorNode = root2.data
self.helper(root1.left, root2.right, target)
self.helper(root1.right, root2.left, target)
def findMirror(self, root, target):
if root.data == target:
return root.data
self.mirrorNode = -1
self.helper(root.left, root.right, target)
self.helper(root.right, root.left, target)
return self.mirrorNode | CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
if root and root.data == target:
return target
if not root:
return -1
global k
k = -1
def sol(root1, root2):
global k
if root1 and root2:
if root1.data == target:
if root2:
k = root2.data
else:
k = -1
if root2.data == target:
if root1:
k = root1.data
else:
k = -1
sol(root1.left, root2.right)
sol(root1.right, root2.left)
sol(root.left, root.right)
if k != -1:
return k
else:
return -1 | CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
q1 = [root]
q2 = [root]
while len(q1) and len(q2):
n = min(len(q1), len(q2))
queue1 = []
queue2 = []
for i in range(n):
node1, node2 = q1[i], q2[i]
if node1 is None or node2 is None:
continue
if node1.data == target:
return node2.data
queue1.append(node1.left)
queue1.append(node1.right)
queue2.append(node2.right)
queue2.append(node2.left)
q1 = queue1
q2 = queue2
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NONE VAR NONE IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NUMBER |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
if not root:
return -1
arr = []
if root.data == target:
return target
self.findMirrorUtil(root.left, target, arr, [-1])
self.findMirrorUtil(root.right, target, arr, [1])
head = root
for i in arr:
if i == -1 and head.right:
head = head.right
elif i == 1 and head.left:
head = head.left
else:
return -1
return head.data
def findMirrorUtil(self, root, target, arr, temp):
if not root:
return
if root.data == target:
arr.extend(temp)
return
self.findMirrorUtil(root.left, target, arr, temp + [-1])
self.findMirrorUtil(root.right, target, arr, temp + [1]) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR LIST IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR LIST NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR RETURN NUMBER RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR LIST NUMBER |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
if root is None:
return None
def search(root, target, path):
if root is None:
return False, []
newPath = path.copy()
newPath.append(root)
if root.data == target:
return True, newPath
found, p = search(root.left, target, newPath)
if found:
return True, p
found, p = search(root.right, target, newPath)
if found:
return True, p
return False, []
found, path = search(root, target, [])
if not found:
return -1
dummyIter = iter = root
path.pop(0)
while path:
try:
node = path.pop(0)
if dummyIter.left == node:
iter = iter.right
else:
iter = iter.left
dummyIter = node
except:
return -1
if iter:
return iter.data
return -1 | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE FUNC_DEF IF VAR NONE RETURN NUMBER LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN NUMBER VAR RETURN NUMBER LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR LIST IF VAR RETURN NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NUMBER IF VAR RETURN VAR RETURN NUMBER |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def asd(self, x, y, target, a):
if x == None or y == None:
return
else:
if x.data == target:
a.append(y.data)
self.asd(x.left, y.right, target, a)
self.asd(x.right, y.left, target, a)
def findMirror(self, root, target):
a = []
self.asd(root, root, target, a)
if a == []:
return -1
return a[0] | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR LIST RETURN NUMBER RETURN VAR NUMBER |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMoves(self, node, target):
if node.data == target:
return [0]
left_moves = right_moves = None
if node.left:
left_moves = self.findMoves(node.left, target)
if node.right:
right_moves = self.findMoves(node.right, target)
if left_moves:
return [-1] + left_moves
if right_moves:
return [1] + right_moves
return None
def findMirror(self, root, target):
moves = self.findMoves(root, target)
node = root
for m in moves[:-1]:
if m == -1:
node = node.right
else:
node = node.left
if not node:
return -1
return node.data | CLASS_DEF FUNC_DEF IF VAR VAR RETURN LIST NUMBER ASSIGN VAR VAR NONE IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN BIN_OP LIST NUMBER VAR IF VAR RETURN BIN_OP LIST NUMBER VAR RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR RETURN NUMBER RETURN VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def helper(self, root):
if root == None:
return
newRoot = Node(root.data)
newRoot.left = self.helper(root.right)
newRoot.right = self.helper(root.left)
return newRoot
def check(self, root, newRoot, k):
if root == None or newRoot == None:
return -1
if root.data == k:
return newRoot.data
ans = self.check(root.left, newRoot.left, k)
ans1 = self.check(root.right, newRoot.right, k)
if ans != -1:
return ans
elif ans1 != -1:
return ans1
return -1
def findMirror(self, root, target):
newRoot = self.helper(root)
return self.check(root, newRoot, target) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
if not root:
return -1
if root.data == target:
return target
mirror = [None]
self._findMirror(root.left, root.right, target, mirror)
return mirror[0] if mirror[0] else -1
def _findMirror(self, left, right, target, mirror):
if not left or not right:
return
if left.data == target:
mirror[0] = right.data
return
if right.data == target:
mirror[0] = left.data
return
self._findMirror(left.left, right.right, target, mirror)
self._findMirror(left.right, right.left, target, mirror) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN VAR ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
ans = -1
def findMirror(self, root, target):
if root.data == target:
return root.data
if root is None:
return -1
else:
self.isMirror(root.left, root.right, target)
return self.ans
def isMirror(self, left, right, target):
if left is None or right is None:
return
if left.data == target:
self.ans = right.data
elif right.data == target:
self.ans = left.data
self.isMirror(left.left, right.right, target)
self.isMirror(left.right, right.left, target) | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def checkMirror(self, left, right):
if not left or not right:
return 0
if left.data == target:
return right.data
if right.data == target:
return left.data
else:
return self.checkMirror(left.left, right.right) + self.checkMirror(
left.right, right.left
)
def findMirror(self, root, target):
if root.data == target:
return root.data
ans = self.checkMirror(root.left, root.right)
if not ans:
return -1
return ans | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER RETURN VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
if root.data == target:
return target
kkkk = self.mirror(root.left, root.right, target)
if kkkk:
return kkkk
return -1
def mirror(self, a, b, target):
if a and b:
if a.data == target:
return b.data
if b.data == target:
return a.data
kk = self.mirror(a.left, b.right, target)
if kk:
return kk
return self.mirror(a.right, b.left, target) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def soln(self, node1, node2, target, m):
if node1 is None or node2 is None:
return
if node1.data == target:
self.m = node2.data
return
if node2.data == target:
self.m = node1.data
return
l = self.soln(node1.left, node2.right, target, self.m)
if self.m != -1:
return
return self.soln(node1.right, node2.left, target, self.m)
def findMirror(self, root, target):
if root is None:
return -1
if root.data == target:
return target
self.m = -1
self.soln(root.left, root.right, target, self.m)
return self.m | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR VAR ASSIGN VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
path = self.findPath(root, target, [])
for item in path:
if item == "left":
root = root.right
else:
root = root.left
if root is None:
return -1
return root.data
def findPath(self, root, target, path):
if root is None:
return []
if root.data == target:
return path
left = self.findPath(root.left, target, path.copy() + ["left"])
right = self.findPath(root.right, target, path.copy() + ["right"])
return left + right | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR LIST FOR VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN LIST IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR LIST STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR LIST STRING RETURN BIN_OP VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def recur(self, r1, r2, target):
global ans
if not r1 or not r2:
return
if r1.data == target:
ans = r2.data
return
elif r2.data == target:
ans = r1.data
return
self.recur(r1.left, r2.right, target)
self.recur(r1.right, r2.left, target)
def findMirror(self, root, target):
if root.data == target:
return root.data
global ans
ans = None
self.recur(root.left, root.right, target)
return ans if ans else -1 | CLASS_DEF FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR NUMBER |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def recur(self, r1, r2, t):
global ans
if not r1 or not r2:
return
if r1.data == t:
if r2:
ans = r2.data
if r1.left and r2.right:
self.recur(r1.left, r2.right, t)
if r1.right and r2.left:
self.recur(r1.right, r2.left, t)
def findMirror(self, root, target):
global ans
ans = -1
self.recur(root, root, target)
return ans | CLASS_DEF FUNC_DEF IF VAR VAR RETURN IF VAR VAR IF VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def Mirror(self, root1, root2, target, ans):
if root1 == None and root2 == None:
return
if root1 == None or root2 == None:
return
if root1.data == target:
ans[0] = root2.data
return
self.Mirror(root1.left, root2.right, target, ans)
self.Mirror(root1.right, root2.left, target, ans)
def findMirror(self, root, target):
ans = [-1]
self.Mirror(root, root, target, ans)
return ans[0] | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR NONE VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
if not root:
return -1
if target == root.data:
return root.data
self.mirror = -1
self.helper(root.left, root.right, target)
return self.mirror
def helper(self, root1, root2, target):
if root1 == None and root2 == None:
return None
if root1 != None and root2 != None:
if root1.data == target:
self.mirror = root2.data
if root2.data == target:
self.mirror = root1.data
if root1 == None:
if root2.data == target:
self.mirror = -1
return
if root2 == None:
if root1.data == target:
self.mirror = -1
return
self.helper(root1.left, root2.right, target)
self.helper(root1.right, root2.left, target) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN NONE IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NONE IF VAR VAR ASSIGN VAR NUMBER RETURN IF VAR NONE IF VAR VAR ASSIGN VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def solve(self, a, b, target):
if not a or not b:
return -1
self.dic[a.data] = b.data
self.dic[b.data] = a.data
self.solve(a.left, b.right, target)
self.solve(a.right, b.left, target)
def findMirror(self, root, target):
self.dic = {}
self.dic[root.data] = root.data
self.solve(root.left, root.right, target)
if target not in self.dic:
return -1
return self.dic[target] | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
def trav(root, p, k):
if root is None:
return 0
if root.data == p:
return 1
y = trav(root.left, p, k)
if y == 1:
k.append(1)
return 1
x = trav(root.right, p, k)
if x == 1:
k.append(-1)
return 1
return 0
k = []
trav(root, target, k)
k.reverse()
for i in k:
if i == 1:
root = root.right
if i == -1:
root = root.left
if root is None:
return -1
return root.data | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NONE RETURN NUMBER RETURN VAR |
Given a Binary tree, the problem is to find mirror of a given node. The mirror of a node is a node which exist at the mirror position of node in opposite subtree at the root.
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
and target = 4
Output: 7
Explanation: You can see below that the mirror
node of 4 is 7.
1 | 1
/ \ | / \
2 3 | 3 2
/ \ / \ | / \ / \
4 5 6 7 | 7 6 5 4
Example 2:
Input:
1
/ \
2 3
/ \
4 5
and target = 4
Output: -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMirror() which takes root node of the tree and a integer target as input parameter and returns the value of the mirror node of the given target node. If the mirror node does not exists return -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=n<=10^{4}
1<=data of node<=10^{4}
1<=target<=10^{4} | class Solution:
def findMirror(self, root, target):
directions = []
get_directions(root, target, [], directions)
return find_node(root, directions[0], 0)
def get_directions(cur, target, path, res):
if not cur:
return
if cur.data == target:
res.append("".join(path))
return
path.append("L")
get_directions(cur.left, target, path, res)
path.pop()
path.append("R")
get_directions(cur.right, target, path, res)
path.pop()
def find_node(cur, directions, i):
if not cur:
return -1
if i == len(directions):
return cur.data
if directions[i] == "R":
return find_node(cur.left, directions, i + 1)
else:
return find_node(cur.right, directions, i + 1) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR LIST VAR RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
global nv
nv = 0
if self.vacc(root) == 1:
nv += 1
return nv
def vacc(self, root):
global nv
l, r = 0, 0
if root is None:
return 0
l = self.vacc(root.left)
r = self.vacc(root.right)
if l == 1 or r == 1:
nv += 1
return 2
elif l == 2 or r == 2:
return 0
else:
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
cnt = []
def solve(root):
if root == None:
return 1
lChild = solve(root.left)
rChild = solve(root.right)
if lChild == -1 or rChild == -1:
cnt.append(1)
return 0
if lChild == 0 or rChild == 0:
return 1
return -1
if solve(root) == -1:
cnt.append(1)
return len(cnt) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def res(self, root, c):
if root is None:
return 0
l = self.res(root.left, c)
r = self.res(root.right, c)
if l == 0 and r == 1:
return 0
if l == 1 and r == 0:
return 0
if l == 0 and r == 0:
return -1
if l == 1 and r == 1:
return 0
if l == -1 or r == -1:
c[0] += 1
return 1
def supplyVaccine(self, root):
c = [0]
l = self.res(root, c)
if l == -1:
c[0] += 1
return c[0] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER RETURN VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Node:
def _init_(self, val):
self.data = val
self.left = None
self.right = None
class Solution:
v = 0
def solve(self, node):
if node == None:
return 0
l = self.solve(node.left)
r = self.solve(node.right)
if l == 1 or r == 1:
self.v += 1
if l == 0:
return r % 3 + 1
if r == 0:
return l % 3 + 1
return min(l, r) % 3 + 1
def supplyVaccine(self, root):
m = self.solve(root)
if m == 1:
self.v += 1
return self.v | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
vaccine = 0
def countVaccine(self, root):
if not root:
return "ok"
left = self.countVaccine(root.left)
right = self.countVaccine(root.right)
if left == "want" or right == "want":
self.vaccine += 1
return "provide"
if left == "provide" or right == "provide":
return "ok"
return "want"
def supplyVaccine(self, root):
if self.countVaccine(root) == "want":
self.vaccine += 1
return self.vaccine | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR NUMBER RETURN STRING IF VAR STRING VAR STRING RETURN STRING RETURN STRING FUNC_DEF IF FUNC_CALL VAR VAR STRING VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | NEED_VACCINE = 0
HAS_VACCINE = 1
NO_NEED_OF_VACCINE = 2
class Solution:
def supplyVaccine(self, root):
vaccineCount = [0]
if self._supplyVaccine(root, vaccineCount) == NEED_VACCINE:
vaccineCount[0] += 1
return vaccineCount[0]
def _supplyVaccine(self, root, vaccineCount):
if not root:
return NO_NEED_OF_VACCINE
left = self._supplyVaccine(root.left, vaccineCount)
right = self._supplyVaccine(root.right, vaccineCount)
if left == NEED_VACCINE or right == NEED_VACCINE:
vaccineCount[0] += 1
return HAS_VACCINE
elif left == HAS_VACCINE or right == HAS_VACCINE:
return NO_NEED_OF_VACCINE
else:
return NEED_VACCINE | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR NUMBER FUNC_DEF IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR IF VAR VAR VAR VAR RETURN VAR RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def Vaccine(self, root):
global cnt
if root == None:
return 1
L = Solution().Vaccine(root.left)
R = Solution().Vaccine(root.right)
if L == 1 and R == 1:
return 2
elif L == 2 or R == 2:
cnt += 1
return 3
return 1
def supplyVaccine(self, root):
global cnt
cnt = 0
value = Solution().Vaccine(root)
return cnt + (1 if value == 2 else 0) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR NUMBER NUMBER NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | global res
class Solution:
def supplyVaccine(self, root):
global res
res = 0
if root == None:
return 0
if dfs(root) == 0:
return 1 + res
else:
return res
def dfs(root):
if root == None:
return 2
lf = dfs(root.left)
rr = dfs(root.right)
if lf == 0 or rr == 0:
global res
res = res + 1
return 1
if lf == 1 or rr == 1:
return 2
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP NUMBER VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def traverse(self, root, count):
if root == None:
return 1
l = self.traverse(root.left, count)
r = self.traverse(root.right, count)
if l == -1 or r == -1:
count[0] += 1
return 0
if l == 0 or r == 0:
return 1
return -1
def supplyVaccine(self, root):
count = [0]
if self.traverse(root, count) == -1:
count[0] += 1
return count[0] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
def dfs(node):
if not node:
return float("inf"), 0, 0
l_kit, l_covered, l_notcovered = dfs(node.left)
r_kit, r_covered, r_notcovered = dfs(node.right)
kit = (
min(l_kit, l_covered, l_notcovered)
+ min(r_kit, r_covered, r_notcovered)
+ 1
)
covered = min(min(l_kit, l_covered) + r_kit, min(r_kit, r_covered) + l_kit)
notcovered = l_covered + r_covered
return kit, covered, notcovered
k, c, nc = dfs(root)
return min(k, c, nc + 1) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
def traverse(head, root, ans):
if not root:
return 0
l = traverse(head, root.left, ans)
r = traverse(head, root.right, ans)
if root == head:
if max(l, r) == -1 and min(l, r) == -1:
return 0
elif max(l, r) == 0 and min(l, r) == -1:
return 0
else:
ans[0] += 1
return 0
if max(l, r) + 1 == 2:
ans[0] += 1
return -1
elif max(l, r) == 0 and min(l, r) == -1:
return 0
else:
return max(l, r) + 1
ans = [0]
traverse(root, root, ans)
return ans[0] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
cnt = [0]
def solve(root):
if not root:
return 0
left = solve(root.left)
right = solve(root.right)
if left == -1 or right == -1:
cnt[0] += 1
return 1
if left == 1 or right == 1:
return 0
return -1
if solve(root) == -1:
cnt[0] += 1
return cnt[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
v = 0
def solve(node):
nonlocal v
if not node:
return 0
l = solve(node.left)
r = solve(node.right)
if l == 1 or r == 1:
v += 1
if l == 0:
return r % 3 + 1
if r == 0:
return l % 3 + 1
return min(l, r) % 3 + 1
m = solve(root)
return v if m != 1 else v + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR BIN_OP VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | novaccine = 0
class Solution:
def supplyVaccine(self, root):
if self.vaccine(root) == "want":
global novaccine
novaccine += 1
ans = novaccine
novaccine = 0
return ans
def vaccine(self, root):
if not root:
return "ok"
left = self.vaccine(root.left)
right = self.vaccine(root.right)
if left == "want" or right == "want":
global novaccine
novaccine += 1
return "provide"
elif left == "provide" or right == "provide":
return "ok"
return "want" | ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR NUMBER RETURN STRING IF VAR STRING VAR STRING RETURN STRING RETURN STRING |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
a, b = self.cnt(root)
return a + b // 2
def cnt(self, node):
if not node.left and not node.right:
return 0, 2
la, lb = self.cnt(node.left) if node.left else (0, 3)
ra, rb = self.cnt(node.right) if node.right else (0, 3)
a = la + ra
if lb == 2 or rb == 2:
a += 1
b = 0
else:
b = min(lb, rb) + 1
return a, b | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | import sys
sys.setrecursionlimit(10**6)
class Solution:
def __init__(self):
self.vaccine = 0
def vaccine_needed(self, root):
if root is None:
return -1
left = self.vaccine_needed(root.left)
right = self.vaccine_needed(root.right)
if left == 1 or right == 1:
self.vaccine = self.vaccine + 1
return 0
if left == 0 or right == 0:
return -1
return 1
def supplyVaccine(self, root):
if self.vaccine_needed(root) == 1:
self.vaccine = self.vaccine + 1
return self.vaccine | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
if not root:
return 0
if not root.left and not root.right:
return 1
self.count = 0
self.needs = 1
self.covered = 2
self.has_vaccine_kit = 3
self.not_need = 4
if self.needs == self.helper(root):
self.count += 1
return self.count
def helper(self, root):
if not root:
return self.not_need
if not root.left and not root.right:
return self.needs
left = self.helper(root.left)
right = self.helper(root.right)
if left == self.needs or right == self.needs:
self.count += 1
return self.has_vaccine_kit
if left == self.has_vaccine_kit or right == self.has_vaccine_kit:
return self.covered
return self.needs | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR IF VAR VAR VAR VAR RETURN VAR RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def __init__(self):
self.vac_count = 0
def rec_count(self, root):
if root == None:
return 0
if root.left == None and root.right == None:
return -1
lc = self.rec_count(root.left)
rc = self.rec_count(root.right)
if lc == -1 or rc == -1:
self.vac_count += 1
return 1
if lc == 0 and rc == 0:
return -1
return 1
if lc == 1 or rc == 1:
return 0
def supplyVaccine(self, root):
_ = self.rec_count(root)
if _ == -1:
return self.vac_count + 1
return self.vac_count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
def dfs(node):
if not node:
return 1
l = dfs(node.left)
r = dfs(node.right)
if l == 0 or r == 0:
self.sum += 1
return 2
elif l == 2 or r == 2:
return 1
else:
return 0
self.sum = 0
if dfs(root) == 0:
self.sum += 1
return self.sum | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
self.vaxKit = 0
res = self.supplyVaccineRecursive(root)
if res == -1:
self.vaxKit += 1
return self.vaxKit
def supplyVaccineRecursive(self, root):
if not (root.left or root.right):
return -1
else:
right = 0
left = 0
if root.left:
left = self.supplyVaccineRecursive(root.left)
if root.right:
right = self.supplyVaccineRecursive(root.right)
if left == -1 or right == -1:
self.vaxKit += 1
return 1
if left == 1 or right == 1:
return 0
if left == 0 and right == 0:
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | ans = 0
def mgc(root):
global ans
if not root:
return 1
left = mgc(root.left)
right = mgc(root.right)
if left == 1 and right == 1:
return 2
elif left == 2 or right == 2:
ans += 1
return 3
return 1
class Solution:
def supplyVaccine(self, root):
global ans
ans = 0
if mgc(root) == 2:
ans += 1
return ans | ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
global ans
ans = 0
if self.vaccine(root) == "want":
ans += 1
return ans
def vaccine(self, root):
global ans
if root == None:
return "ok"
left = self.vaccine(root.left)
right = self.vaccine(root.right)
if left == "want" or right == "want":
ans += 1
return "provide"
if left == "provide" or right == "provide":
return "ok"
return "want" | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR STRING VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR NUMBER RETURN STRING IF VAR STRING VAR STRING RETURN STRING RETURN STRING |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
number = 0
def rec_count_vaccine(self, root):
if root is None:
return True, False
L_got_vac, L_own_vac = self.rec_count_vaccine(root.left)
R_got_vac, R_own_vac = self.rec_count_vaccine(root.right)
root_got_vac = False
root_own_vac = False
if L_own_vac or R_own_vac:
root_got_vac = True
if L_got_vac == False or R_got_vac == False:
root_own_vac = True
root_got_vac = True
self.number += 1
return root_got_vac, root_own_vac
def supplyVaccine(self, root):
got_vac, own_vac = self.rec_count_vaccine(root)
if got_vac == False:
self.number += 1
return self.number | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | is_fed = {None: False}
def check_fed(node):
if node in is_fed.keys():
return True
return False
def feed(gp, root, cnt):
if root is None:
return cnt
cnt = feed(root, root.left, cnt)
cnt = feed(root, root.right, cnt)
if (
root.left is not None
and not check_fed(root.left)
or root.right is not None
and not check_fed(root.right)
):
is_fed[gp] = True
is_fed[root] = True
is_fed[root.right] = True
is_fed[root.left] = True
return cnt + 1
return cnt
class Solution:
def supplyVaccine(self, root):
is_fed.clear()
cnt = feed(None, root, 0)
if not check_fed(root):
return cnt + 1
return cnt | ASSIGN VAR DICT NONE NUMBER FUNC_DEF IF VAR FUNC_CALL VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE FUNC_CALL VAR VAR VAR NONE FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NONE VAR NUMBER IF FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
self.ans = 0
covered = {None}
def dfs(node, par=None):
if node:
dfs(node.left, node)
dfs(node.right, node)
if (
par is None
and node not in covered
or node.left not in covered
or node.right not in covered
):
self.ans += 1
covered.update({node, par, node.left, node.right})
dfs(root)
return self.ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE FUNC_DEF NONE IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NONE VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def traverse(self, n):
if not n:
return 0, 2
vaccineKitL, distL = self.traverse(n.left)
vaccineKitR, distR = self.traverse(n.right)
if max(distL, distR) == 3:
return vaccineKitL + vaccineKitR + 1, 1
return vaccineKitL + vaccineKitR, 1 + min(distL, distR)
def supplyVaccine(self, root):
vaccine_kits, distance = self.traverse(root)
if distance == 3:
return vaccine_kits + 1
return vaccine_kits | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def util(self, root, ans):
if root is None:
return "mnc"
left = self.util(root.left, ans)
right = self.util(root.right, ans)
if left == "nm" or right == "nm":
ans[0] += 1
return "mwc"
if left == "mnc" and right == "mnc":
return "nm"
return "mnc"
def supplyVaccine(self, root):
ans = [0]
z = self.util(root, ans)
if z == "nm":
ans[0] += 1
return ans[0] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING VAR NUMBER NUMBER RETURN STRING IF VAR STRING VAR STRING RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER NUMBER RETURN VAR NUMBER |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
self.count = 0
def supply(par, root):
if not root:
return False
if not root.left and not root.right:
return True
if supply(root, root.left):
root.data = 0
self.count += 1
flag = supply(root, root.right)
if root.data != 0 and flag:
root.data = 0
self.count += 1
if root.data != 0:
if root.left and root.left.data == 0:
return False
if root.right and root.right.data == 0:
return False
if not par:
self.count += 1
return True
return False
supply(None, root)
return self.count | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NONE VAR RETURN VAR |
Geek has developed an effective vaccine for Corona virus and he wants each of the N houses in Geek Land to have access to it. Given a binary tree where each node represents a house in Geek Land, find the minimum number of houses that should be supplied with the vaccine kit if one vaccine kit is sufficient for that house, its parent house and it's immediate child nodes.
Example 1:
Input:
1
/ \
2 3
\
4
\
5
\
6
Output:
2
Explanation:
The vaccine kits should be
supplied to house numbers 1 and 5.
Example 2:
Input:
1
/ \
2 3
Output:
1
Explanation:
The vaccine kits should be
supplied to house number 1.
Your Task:
You don't need to read input or print anything. Complete the function supplyVaccine() which takes the root of the housing tree as input parameter and returns the minimum number of houses that should be supplied with the vaccine kit.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5} | class Solution:
def supplyVaccine(self, root):
ans = [0]
if self.recursion(root, ans) == 0:
ans[0] += 1
return ans[0]
def recursion(self, root, ans):
if root is None:
return 1
left = self.recursion(root.left, ans)
right = self.recursion(root.right, ans)
if left == 0 or right == 0:
ans[0] += 1
return 2
elif left == 2 or right == 2:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER 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 DUS(object):
def __init__(self, n):
self.p = [*list(range(n))]
self.size = [1] * n
def Find(self, x):
if self.p[x] == x:
return x
tmp = []
while self.p[x] != x:
tmp.append(x)
x = self.p[x]
for t in tmp:
self.p[t] = x
return x
def Union(self, x, y):
xr = self.Find(x)
yr = self.Find(y)
if xr != yr:
self.p[yr] = xr
self.size[xr] += self.size[yr]
self.size[yr] = 0
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def PFD(num):
ret = set()
if num % 2 == 0:
ret.add(2)
while num % 2 == 0:
num //= 2
for i in range(3, int(num**0.5) + 1, 2):
if num % i == 0:
ret.add(i)
while num % i == 0:
num //= i
if num > 2:
ret.add(num)
return ret
dic = {}
dus = DUS(len(A))
for idx, a in enumerate(A):
for p in PFD(a):
if p in dic:
dus.Union(dic[p], idx)
dic[p] = idx
return max(dus.size) | CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR LIST WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL 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 BIN_OP VAR NUMBER NUMBER NUMBER 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 ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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 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(object):
def uf(self, n):
self.uf = [i for i in range(n)]
self.size = [1] * n
def find(self, x):
while x != self.uf[x]:
self.uf[x] = self.uf[self.uf[x]]
x = self.uf[x]
return self.uf[x]
def union(self, x, y):
x_root = self.find(x)
y_root = self.find(y)
if x_root == y_root:
return
self.uf[x_root] = y_root
self.size[y_root] += self.size[x_root]
self.size[x_root] = 0
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def primeFactors(n):
out = set()
while n % 2 == 0:
out.add(2)
n //= 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
out.add(i)
n //= i
if n > 2:
out.add(n)
return out
idx_lookup = {A[i]: i for i in range(len(A))}
uf = UnionFind()
uf.uf(len(A))
primeAndItsMultiples = collections.defaultdict(list)
for i in A:
factors = primeFactors(i)
for f in factors:
primeAndItsMultiples[f].append(i)
for idx, multiples in primeAndItsMultiples.items():
if multiples:
root = multiples[0]
for node in multiples[1:]:
uf.union(idx_lookup[node], idx_lookup[root])
return max(uf.size) | CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR 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 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 ASSIGN VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER 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 VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR 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 FU:
def __init__(self, n):
self.p = [i for i in 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):
px, py = self.find(x), self.find(y)
self.p[py] = px
class Solution:
def primeFactors(self, n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return self.primeFactors(n // i) | set([i])
return set([n])
def largestComponentSize(self, A: List[int]) -> int:
fu = FU(len(A))
primes = defaultdict(list)
for i in range(len(A)):
prime = self.primeFactors(A[i])
for pr in prime:
primes[pr].append(i)
for k, v in list(primes.items()):
for i in range(len(v) - 1):
fu.union(v[i], v[i + 1])
c = Counter([fu.find(i) for i in range(len(A))])
return max(c.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN 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 FUNC_CALL 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 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 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 DSU:
def __init__(self, count):
self.parent = [i for i in range(count)]
self.size = [(1) for _ in range(count)]
self.max = 0
def find(self, x):
root = x
while root != self.parent[root]:
root = self.parent[root]
while x != root:
next_root = self.parent[x]
self.parent[x] = root
x = next_root
return root
def union(self, x, y):
r1, r2 = self.find(x), self.find(y)
if r1 == r2:
return
if self.size[r1] < self.size[r2]:
self.size[r2] += self.size[r1]
self.parent[r1] = r2
else:
self.size[r1] += self.size[r2]
self.parent[r2] = r1
self.max = max(self.max, self.size[r1], self.size[r2])
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
dsu = DSU(len(A))
def getPrimeFactors(n):
res = set()
while n % 2 == 0:
res.add(2)
n //= 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
res.add(i)
n //= i
if n > 2:
res.add(n)
return res
prime_to_idx = {}
for i, n in enumerate(A):
primes = getPrimeFactors(n)
for prime in primes:
if prime in prime_to_idx:
dsu.union(i, prime_to_idx[prime])
prime_to_idx[prime] = i
return dsu.max | 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 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 IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER 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 VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN 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 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 = [-1] * 1000001
def _find(a):
if parent[a] == -1:
return a
parent[a] = _find(parent[a])
return parent[a]
def _union(a, b):
ap = _find(a)
bp = _find(b)
if ap != bp:
parent[bp] = ap
for a in A:
for i in range(2, int(sqrt(a)) + 1):
if a % i == 0:
_union(i, a)
_union(a, a // i)
cnt = 0
tmp = {}
for a in A:
ap = _find(a)
cnt = max(cnt, 1 + tmp.get(ap, 0))
tmp[ap] = 1 + tmp.get(ap, 0)
return cnt | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR NUMBER 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 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 NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL 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 = [-1] * 100001
def find(x):
if parent[x] == -1:
return x
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
xp, yp = find(x), find(y)
if xp != yp:
parent[yp] = xp
for x in A:
for i in range(2, int(sqrt(x)) + 1):
if x % i == 0:
union(i, x)
union(x, x // i)
count = 0
cache = dict()
for x in A:
xp = find(x)
count = max(count, 1 + cache.get(xp, 0))
cache[xp] = 1 + cache.get(xp, 0)
return count
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
graph = dict()
for a in A:
graph[a] = list()
n = len(A)
for i in range(n - 1):
for j in range(i + 1, n):
a, b = A[i], A[j]
if gcd(a, b) > 1:
graph[a].append(b)
for a in graph.keys():
print(f"graph[{a}]", graph[a])
largest = float("-inf")
def dfs(graph, node, count):
nonlocal largest
print("node", node, "count", count, "largest", largest)
if not graph[node]:
largest = max(largest, count)
return
for c in graph[node]:
dfs(graph, c, count + 1)
for a in A:
dfs(graph, a, 1)
print(largest)
print("-" * 50) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER 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 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 NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING 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 Primes:
def __init__(self, N):
self.N = N
self.primes = [0] * (N + 1)
self.get_primes()
def get_primes(self):
for i in range(2, self.N + 1):
if self.primes[i] == 0:
self.primes[i] = i
upto = self.N // i
for j in range(2, upto + 1):
if self.primes[i * j] == 0:
self.primes[i * j] = i
def __call__(self, n):
if n <= 2:
return {n}
p = []
while n >= 2:
lp = self.primes[n]
p.append(lp)
n = n // lp
return set(p)
class DisjointSet:
def __init__(self, primes):
self.primes = primes
self.count = 1
def union(self, setB):
self.primes |= setB.primes
self.count += setB.count
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
disjoint_sets = []
Factors = Primes(max(A))
rank = [1] * (len(A) + 1)
parent = [-1] * (len(A) + 1)
m = dict()
curr_count = 0
def get_parent(x):
if parent[x] == -1:
return x
parent[x] = get_parent(parent[x])
return parent[x]
def union(x, y):
parx = get_parent(x)
pary = get_parent(y)
if parx == pary:
return
if rank[parx] >= rank[pary]:
rank[parx] += rank[pary]
parent[pary] = parx
else:
rank[pary] += rank[parx]
parent[parx] = pary
for i, val in enumerate(A):
val_primes = Factors(val)
for prime in val_primes:
if prime in m:
union(m[prime], i)
else:
m[prime] = i
curr_count = max(rank)
return curr_count | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR NUMBER 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 IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR 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 ASSIGN VAR FUNC_CALL 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:
count = [(1) for _ in A]
parent = [i for i in range(len(A))]
def find_it(index):
while index != parent[index]:
index = parent[index]
return index
def union_it(index1, index2):
p, q = find_it(index1), find_it(index2)
if p == q:
return
if count[p] < count[q]:
p, q = q, p
count[p] += count[q]
parent[q] = p
top = floor(sqrt(max(A)))
is_prime = [(True) for _ in range(1 + top)]
is_prime[0], is_prime[1] = False, False
for p in range(2, len(is_prime)):
if not is_prime[p]:
continue
k = -1
for i in range(len(A)):
if A[i] % p:
continue
while A[i] % p == 0:
A[i] //= p
if k < 0:
k = i
else:
union_it(k, i)
p1 = 2 * p
while p1 <= top:
is_prime[p1] = False
p1 += p
AA = [(n, i) for i, n in enumerate(A) if n > 1]
AA.sort()
for index in range(1, len(AA)):
if AA[index - 1][0] == AA[index][0]:
union_it(AA[index - 1][1], AA[index][1])
return max(count) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR 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:
self.uf_table = [idx for idx in range(100001)]
def find(p):
if self.uf_table[p] != p:
self.uf_table[p] = find(self.uf_table[p])
return self.uf_table[p]
def union(p, q):
rootp = find(p)
rootq = find(q)
if rootp != rootq:
self.uf_table[rootp] = rootq
for x in A:
for factor in range(2, int(sqrt(x)) + 1):
if x % factor == 0:
union(x, factor)
union(x, x // factor)
results = [(0) for _ in range(100001)]
for x in A:
results[find(x)] += 1
return max(results) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR 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 FUNC_CALL VAR VAR ASSIGN 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 NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR 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 | 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,
]
def prime_factors(n: int) -> List[int]:
if n == 1:
return []
for p in primes:
if n % p == 0:
return [p] + prime_factors(n // p)
return [n]
def group_by_factors(A):
groups = defaultdict(set)
for a in A:
for p in prime_factors(a):
groups[p].add(a)
return list(groups.values())
def adjacencies_by_intersection(gs):
adj = [[] for _ in range(len(gs))]
for x in range(len(gs)):
for y in range(x):
if gs[x] & gs[y]:
adj[x].append(y)
adj[y].append(x)
return adj
def bfs(gs, adj, target):
unvisited = set(range(len(gs)))
largest = 0
while unvisited:
v = unvisited.pop()
q = [v]
group = set()
while q:
n = q.pop()
group.add(n)
for x in adj[n]:
if x in unvisited:
unvisited.remove(x)
q.append(x)
largest = max(largest, len(set(n for i in group for n in gs[i])))
if largest >= target:
return largest
return largest
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
gs = [g for g in group_by_factors(A) if len(g) > 1]
adj = adjacencies_by_intersection(gs)
target = len(A) // 2
return bfs(gs, adj, target) | 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 FUNC_DEF VAR IF VAR NUMBER RETURN LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN LIST VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN 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:
def prime_factors(n):
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return prime_factors(n // i) | set([i])
return set([n])
f = list(range(len(A)))
def find(x):
if f[x] != x:
f[x] = find(f[x])
return f[x]
def union(x, y):
f[find(x)] = find(y)
p2i = dict()
for i, a in enumerate(A):
primes = prime_factors(a)
for p in primes:
if p in p2i:
union(i, p2i[p])
else:
p2i[p] = find(i)
counts = Counter(map(find, range(len(A))))
return counts.most_common(1)[0][1] | 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 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 ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL 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 Solution:
def largestComponentSize(self, A: List[int]) -> int:
uf = {}
rank = {i: (1) for i in range(len(A))}
def find(x):
uf.setdefault(x, x)
if uf[x] != x:
uf[x] = find(uf[x])
return uf[x]
def union(x, y):
px, py = find(x), find(y)
if px != py:
if rank[px] > rank[py]:
px, py = py, px
rank[py] += rank[px]
uf[px] = py
def primeFactor(x):
ans = set()
if not x % 2:
ans.add(2)
while not x % 2:
x //= 2
for k in range(3, int(sqrt(x)) + 1, 2):
if not x % k:
ans.add(k)
while not x % k:
x //= k
if x > 1:
ans.add(x)
return ans
dic = {}
for i in range(len(A)):
for k in primeFactor(A[i]):
dic.setdefault(k, i)
union(i, dic[k])
return max(Counter([find(i) for i in range(len(A))]).values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR 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 EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL 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 DSU:
def __init__(self, n):
self.p = list(range(n))
self.sz = [1] * n
def find(self, x):
if x == self.p[x]:
return x
self.p[x] = self.find(self.p[x])
return self.p[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.sz[x] < self.sz[y]:
x, y = y, x
self.p[y] = x
self.sz[x] += self.sz[y]
class Solution:
def largestComponentSize(self, a: List[int]) -> int:
N = max(a) + 1
idx = [-1] * N
for i, x in enumerate(a):
idx[x] = i
dsu = DSU(N)
siv = [True] * N
for i in range(2, N):
if siv[i]:
root = idx[i]
for j in range(2 * i, N, i):
siv[j] = False
if idx[j] != -1:
if root == -1:
root = idx[j]
dsu.unite(root, idx[j])
return max(dsu.sz) | 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 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 IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN 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.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, yr = self.find(x), self.find(y)
self.p[xr] = yr
class Solution:
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 largestComponentSize(self, A):
n = len(A)
UF = 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):
UF.union(indexes[i], indexes[i + 1])
return max(Counter([UF.find(i) for i in range(n)]).values())
class UnionFind:
def __init__(self, A):
self.factorparent = []
for i in range(A):
self.factorparent.append(i)
def union(self, x, y):
xp = self.find(x)
yp = self.find(y)
self.factorparent[xp] = yp
def find(self, x):
if x != self.factorparent[x]:
self.factorparent[x] = self.find(self.factorparent[x])
return self.factorparent[x]
class Solution1:
def largestComponentSize(self, A: List[int]) -> int:
if not A:
return 0
length = len(A)
obj = UnionFind(length)
primes = defaultdict(list)
for i, n in enumerate(A):
primeset = self.getPrime(n)
for p in primeset:
primes[p].append(i)
for k, v in list(primes.items()):
for i in range(len(v) - 1):
obj.union(v[i], v[i + 1])
primes = {}
maxval = 0
for i in range(length):
val = obj.find(i)
primes[val] = primes.get(val, 0) + 1
maxval = max(maxval, primes[val])
return maxval
def getPrime(self, n):
i = 2
while i <= int(sqrt(n)):
if n % i == 0:
return self.getPrime(n // i) | set([i])
i += 1
return set([n]) | 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN 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 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 FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN 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 IF VAR RETURN NUMBER 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 ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR VAR NUMBER RETURN FUNC_CALL VAR LIST 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, A):
self._arr = {v: (-1) for v in A}
def union(self, u, v):
p1 = self.find(u)
p2 = self.find(v)
if p1 != p2:
self._arr[p1] = p2
return True
return False
def find(self, u):
if self._arr.get(u, -1) == -1:
return u
self._arr[u] = self.find(self._arr[u])
return self._arr[u]
class Solution:
def factorsOf(self, n):
factors = set()
while n % 2 == 0:
factors.add(2)
n = n // 2
for i in range(3, int(sqrt(n)) + 1, 2):
while n % i == 0:
factors.add(i)
n = n // i
if n > 2:
factors.add(n)
return factors
def largestComponentSize(self, A: List[int]) -> int:
dsu = UnionFind(A)
for n in A:
for factor in self.factorsOf(n):
dsu.union(n, factor)
cnt_map = collections.Counter()
ans = 0
for v in A:
p = dsu.find(v)
cnt_map[p] += 1
ans = max(cnt_map[p], ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR 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 FOR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER 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 Solution:
def largestComponentSize(self, A: List[int]) -> int:
def find(u):
if p[u] == -1:
return u
p[u] = find(p[u])
return p[u]
def union(u, v):
u = find(u)
v = find(v)
if u != v:
if r[u] > r[v]:
p[v] = u
elif r[u] < r[v]:
p[u] = v
else:
p[v] = u
r[u] += 1
n = len(A)
m = max(A) + 1
p = [-1] * m
r = [0] * m
for i in range(n):
factor = 2
num = A[i]
while num >= factor * factor:
if num % factor == 0:
num //= factor
union(A[i], factor)
else:
factor += 1
union(A[i], num)
s = [0] * m
for i in range(n):
s[find(A[i])] += 1
return max(s) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR NUMBER 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 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR 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 get_prime_numbers(self, num_to_idx_dict, max_num: int) -> List[int]:
is_prime_list = [True] * (max_num + 1)
prime_to_nums_dict = defaultdict(list)
for i in range(2, max_num // 2 + 1):
if is_prime_list[i] == False:
continue
num = i
while num <= max_num:
if num in num_to_idx_dict:
prime_to_nums_dict[i].append(num_to_idx_dict[num])
is_prime_list[num] = False
num += i
return prime_to_nums_dict
def find(self, union_list, index: int) -> int:
if index == union_list[index]:
return index
parent = self.find(union_list, union_list[index])
union_list[index] = parent
return parent
def union(self, union_list, index1: int, index2: int) -> None:
union1, union2 = self.find(union_list, index1), self.find(union_list, index2)
union_list[union1] = union2
def largestComponentSize(self, A: List[int]) -> int:
if len(A) == 0:
return 0
max_num = max(A)
union_list = [i for i in range(len(A))]
count_list = [0] * len(A)
num_to_idx_dict = dict(list(zip(A, list(range(len(A))))))
prime_to_nums_dict = self.get_prime_numbers(num_to_idx_dict, max_num)
for prime, nums in list(prime_to_nums_dict.items()):
for i in range(len(nums) - 1):
self.union(union_list, nums[i], nums[i + 1])
for i, num in enumerate(A):
count_list[self.find(union_list, i)] += 1
return max(count_list) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NONE FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR 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 VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR 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,
]
graph = defaultdict(list)
divs = defaultdict(list)
for i in A:
n = i
for k in primes:
if k > n:
break
if n % k == 0:
graph[k].append(i)
divs[i].append(k)
while n % k == 0:
n = n // k
if n > 1:
graph[n].append(i)
divs[i].append(n)
nodes = {i: (0) for i in A}
seen = set()
cnt = 1
def dfs(node):
nodes[node] = cnt
for div in divs[node]:
if div not in seen:
seen.add(div)
for each in graph[div]:
if nodes[each] == 0:
dfs(each)
for i in A:
if nodes[i] == 0:
dfs(i)
cnt += 1
return max(Counter(nodes.values()).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 ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR 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 ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL 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 DisjointSetUnion(object):
def __init__(self, size):
self.parent = [i for i in range(size + 1)]
self.size = [1] * (size + 1)
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px == py:
return px
if self.size[px] > self.size[py]:
px, py = py, px
self.parent[px] = py
self.size[py] += self.size[px]
return py
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:
dsu = DisjointSetUnion(max(A))
num_factor_map = {}
for num in A:
prime_factors = list(set(self.primeDecompose(num)))
num_factor_map[num] = prime_factors[0]
for i in range(0, len(prime_factors) - 1):
dsu.union(prime_factors[i], prime_factors[i + 1])
max_size = 0
group_count = defaultdict(int)
for num in A:
group_id = dsu.find(num_factor_map[num])
group_count[group_id] += 1
max_size = max(max_size, group_count[group_id])
return max_size | CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP 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 VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT 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 NUMBER 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 | max_num = 100000
smallest_prime_factors = [None] * (max_num + 1)
smallest_prime_factors[1] = 1
for num in range(2, max_num + 1):
if smallest_prime_factors[num] is not None:
continue
smallest_prime_factors[num] = num
for multiple in range(num * num, max_num + 1, num):
if smallest_prime_factors[multiple] is None:
smallest_prime_factors[multiple] = num
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
parents = {}
def find(x):
if x not in parents:
parents[x] = x
elif parents[x] != x:
parents[x] = find(parents[x])
return parents[x]
def union(x, y):
xx, yy = find(x), find(y)
if xx != yy:
parents[xx] = yy
def decompose(num):
if num == 1:
return [1]
prime_factor_set = set()
while num != 1:
prime_factor_set.add(smallest_prime_factors[num])
num //= smallest_prime_factors[num]
return list(prime_factor_set)
factor = {}
for num in A:
prime_factors = decompose(num)
factor[num] = prime_factors[0]
for i in range(1, len(prime_factors)):
union(prime_factors[i - 1], prime_factors[i])
counts = Counter(find(factor[num]) for num in A)
return max(counts.values() or [0]) | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NONE ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR NONE ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR 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 ASSIGN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER 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 VAR RETURN FUNC_CALL VAR FUNC_CALL VAR LIST 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 UnionFind:
def __init__(self, length):
self.parents = {num: num for num in range(length)}
self.ranks = {num: (1) for num in range(length)}
def find(self, src):
if self.parents[src] == src:
return src
self.parents[src] = self.find(self.parents[src])
return self.parents[src]
def union(self, src, dest):
rootSrc, rootDest = self.find(src), self.find(dest)
if rootSrc == rootDest:
return -1
if self.ranks[rootSrc] > self.ranks[rootDest]:
self.parents[rootDest] = rootSrc
self.ranks[rootSrc] += self.ranks[rootDest]
return self.ranks[rootSrc]
else:
self.parents[rootSrc] = rootDest
self.ranks[rootDest] += self.ranks[rootSrc]
return self.ranks[rootDest]
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def genPrimes(num):
i = 2
while i * i <= num:
if num % i == 0:
return set([i]) | genPrimes(num // i)
i += 1
return set([num])
uf = UnionFind(len(A))
primes = defaultdict(list)
for i, num in enumerate(A):
primesNum = genPrimes(num)
for prime in primesNum:
primes[prime].append(i)
for _, indexes in primes.items():
for i in range(len(indexes) - 1):
uf.union(indexes[i], indexes[i + 1])
return max(uf.ranks.values()) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR 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 RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR LIST VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL 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 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 UnionFindSet:
def __init__(self, n):
self.parents = list(range(n))
self.ranks = [1] * n
def find(self, u):
if u != self.parents[u]:
self.parents[u] = self.find(self.parents[u])
return self.parents[u]
def union(self, u, v):
pu, pv = self.find(u), self.find(v)
if pu == pv:
return False
if self.ranks[pu] > self.ranks[pv]:
self.parents[pv] = pu
self.ranks[pu] += self.ranks[pv]
elif self.ranks[pu] < self.ranks[pv]:
self.parents[pu] = pv
self.ranks[pv] += self.ranks[pu]
else:
self.parents[pu] = pv
self.ranks[pv] += self.ranks[pu]
return True
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def get_prime_factors(n):
ans = set()
while n % 2 == 0:
ans.add(2)
n //= 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
ans.add(i)
n //= i
if n > 2:
ans.add(n)
return ans
n = len(A)
uf = UnionFindSet(n)
dic = {}
for i, a in enumerate(A):
primes = get_prime_factors(a)
for p in primes:
if p in dic:
uf.union(i, dic[p])
dic[p] = i
return max(uf.ranks) | 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER 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 VAR VAR IF VAR NUMBER 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 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 UFS:
def __init__(self, N):
self._parent = list(range(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, x, y):
xroot, yroot = self.find(x), self.find(y)
if xroot != yroot:
self._parent[xroot] = yroot
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
def primeFactors(k):
out = set()
while k % 2 == 0:
out.add(2)
k //= 2
for i in range(3, int(math.sqrt(k)) + 1, 2):
while k % i == 0:
out.add(i)
k //= i
if k > 2:
out.add(k)
return out
n = len(A)
maxa = max(A)
ufs = UFS(maxa + 1)
for cur in A:
factors = primeFactors(cur)
for f in factors:
ufs.union(f, cur)
count = [0] * (maxa + 1)
for cur in A:
count[ufs.find(cur)] += 1
return max(count) | 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER 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 VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR 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:
factor = {}
dsu = DSU()
def get_primes(num):
primes = set()
i = 2
while i * i <= num:
if num % i == 0:
while num % i == 0:
num = num // i
primes.add(i)
i += 1
if num > 1:
primes.add(num)
return primes
for num in A:
primes = get_primes(num)
for p in primes:
factor.setdefault(p, num)
if dsu.find(num) != dsu.find(factor[p]):
dsu.union(num, factor[p])
ans = 0
for v in list(factor.values()):
ans = max(dsu.count[v], ans)
return ans
class DSU:
def __init__(self):
self.father = {}
self.count = {}
def find(self, a):
self.father.setdefault(a, a)
self.count.setdefault(a, 1)
if a != self.father[a]:
self.father[a] = self.find(self.father[a])
return self.father[a]
def union(self, a, b):
_a = self.find(a)
_b = self.find(b)
if _a != _b:
self.father[_a] = _b
self.count[_b] += self.count[_a] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER 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 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 | class Solution:
def largestComponentSize(self, A: List[int]) -> int:
f = {}
def find(x):
f.setdefault(x, x)
if x != f[x]:
f[x] = find(f[x])
return f[x]
def union(x, y):
px, py = find(x), find(y)
if px != py:
f[px] = py
def get_primes(n):
for i in range(2, int(sqrt(n)) + 1):
if n % i == 0:
return get_primes(n // i) | {i}
return {n}
primes_dict = collections.defaultdict(list)
for i, a in enumerate(A):
primes = get_primes(a)
for p in primes:
primes_dict[p].append(i)
for k, indexes in list(primes_dict.items()):
for j in range(len(indexes) - 1):
union(indexes[j], indexes[j + 1])
return max(collections.Counter(find(i) for i in range(len(A))).values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF 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 FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN 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 FUNC_CALL FUNC_CALL VAR FUNC_CALL 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 Solution:
def largestComponentSize(self, A: List[int]) -> int:
n = len(A)
cnt = dict([(num, 1) for num in A])
par = dict([(num, num) for num in A])
def find(num):
nonlocal par
if par[num] != num:
par[num] = find(par[num])
return par[num]
def union(num, num2):
root, root2 = find(num), find(num2)
if root == root2:
return
if root > root2:
root, root2 = root2, root
par[root2] = root
cnt[root] = cnt[root] + cnt[root2]
cnt[root2] = 0
primes = [2]
i = 3
maxA = max(A)
while i * i <= 100000:
is_prime = True
for prime in primes:
if i % prime == 0:
is_prime = False
break
if is_prime:
primes.append(i)
i += 1
A = sorted(A)
prime_set = {}
for num in A:
prime_set[num] = set()
tmp = num
for prime in primes:
can_div = False
while tmp % prime == 0:
tmp /= prime
can_div = True
if can_div:
prime_set[num].add(prime)
if prime * prime > num:
break
if tmp > 1:
prime_set[num].add(tmp)
prime_nums = {}
for num in prime_set:
for p in prime_set[num]:
prime_nums.setdefault(p, [])
prime_nums[p].append(num)
for prime, nums in prime_nums.items():
roots = set(find(num) for num in nums)
if len(roots) <= 1:
continue
roots = sorted(list(roots))
for root in roots[1:]:
union(roots[0], root)
return max(cnt.values()) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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 IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER 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 decompose(n):
def rec(n, start):
for i in range(start, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return rec(n // i, i) | {i}
return {n}
if n % 2 == 0:
while n % 2 == 0:
n //= 2
if n == 1:
return {2}
return {2} | rec(n, 3)
return rec(n, 3)
prime_set = collections.defaultdict(list)
for i in range(len(A)):
for f in decompose(A[i]):
prime_set[f].append(i)
ls = list(range(len(A)))
def find(x):
if ls[x] != x:
ls[x] = find(ls[x])
return ls[x]
def union(x, y):
ls[find(x)] = find(y)
for v in prime_set.values():
for i in range(len(v) - 1):
union(v[i], v[i + 1])
return max(Counter([find(i) for i in range(len(A))]).values()) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR IF BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR 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 VAR FUNC_CALL VAR VAR FOR 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 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, maxval):
self.arr = [i for i in range(maxval)]
def find(self, n):
if n != self.arr[n]:
self.arr[n] = self.find(self.arr[n])
return self.arr[n]
def union(self, a, b):
self.arr[self.find(a)] = self.arr[self.find(b)]
class Solution:
def largestComponentSize(self, A: List[int]) -> int:
maxval = max(A) + 1
oUnionFind = UnionFind(maxval)
for i in A:
for j in range(2, int(math.sqrt(i)) + 1):
if i % j == 0:
oUnionFind.union(i, j)
oUnionFind.union(i, i // j)
counter = collections.defaultdict(int)
for i in A:
counter[oUnionFind.find(i)] += 1
return max(list(counter.values())) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR 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 VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL 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 RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.