description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
global i
i = 0
def inorder1(root, arr):
global i
if not root:
return 0
inorder1(root.left, arr)
root.data = arr.pop(0)
inorder1(root.right, arr)
def inorder(root):
if not root:
return []
return inorder(root.left) + [root.data] + inorder(root.right)
arr = inorder(root)
prev = 0
sum1 = sum(arr)
for i in range(len(arr)):
a = arr[i]
arr[i] = sum1 - prev
prev += a
inorder1(root, arr)
return root | FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def util(root):
global v
if root == None:
return
util(root.right)
root.data += v
v = root.data
util(root.left)
def modify(root):
global v
v = 0
util(root)
return root | FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def _modify(root, sum):
if root != None:
_modify(root.right, sum)
sum[0] += root.data
root.data = sum[0]
_modify(root.left, sum)
def modify(root):
sum = [0]
_modify(root, sum)
return root | FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
arr = items(root)
store = {}
summa = 0
for item in arr:
summa += item
store[item] = summa
stack = [root]
while stack:
tree = stack.pop()
if not tree:
continue
temp = tree.data
tree.data = store[temp]
stack.append(tree.left)
stack.append(tree.right)
return root
def items(root):
if root is None:
return []
output = []
output += items(root.right)
output += [root.data]
output += items(root.left)
return output | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR VAR LIST VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
sum = [0]
if root is None:
return
addingVal(root, sum)
return root
def addingVal(root, sum):
if root is None:
return
addingVal(root.right, sum)
sum[0] = sum[0] + root.data
root.data = sum[0]
addingVal(root.left, sum) | FUNC_DEF ASSIGN VAR LIST NUMBER IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def treeinorder(root, l):
if root == None:
return []
else:
treeinorder(root.left, l)
l.append(root.data)
treeinorder(root.right, l)
return l
def solve(root, d):
if root == None:
return None
else:
root.data = d[root.data]
solve(root.left, d)
solve(root.right, d)
return root
def modify(root):
l = []
treeinorder(root, l)
d = {}
a = sum(l)
b = l[0]
d[l[0]] = a
for i in range(1, len(l)):
d[l[i]] = a - b
b = b + l[i]
return solve(root, d) | FUNC_DEF IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
arr = []
def getInorder(root):
if root == None:
return
getInorder(root.left)
arr.append(root.data)
getInorder(root.right)
getInorder(root)
s = 0
for i in range(len(arr) - 1, -1, -1):
s += arr[i]
arr[i] = s
index = 0
def modify(root):
if root == None:
return
modify(root.left)
if len(arr) > 0:
root.data = arr[0]
del arr[0]
modify(root.right)
modify(root)
return root | FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
q = []
cur = root
temp = 0
while True:
if cur is not None:
q.append(cur)
cur = cur.right
elif q:
cur = q.pop()
cur.data += temp
temp = cur.data
cur = cur.left
else:
break
return root | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
def modify(root):
cursum = 0
def inorder(root):
if root is None:
return None
else:
nonlocal cursum
inorder(root.right)
temp = root.data
root.data += cursum
cursum += temp
inorder(root.left)
return root
return inorder(root) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
ans = []
sum = 0
def dfs(root):
nonlocal sum
if not root:
return
dfs(root.right)
sum += root.data
root.data = sum
dfs(root.left)
dfs(root)
return root | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
traver(root, 0)
return root
def traver(root, n):
if root is None:
return n
n = traver(root.right, n)
n += root.data
root.data = n
n = traver(root.left, n)
return n | FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modifyBSTUtil(root, Sum):
if root == None:
return
modifyBSTUtil(root.right, Sum)
Sum[0] = Sum[0] + root.data
root.data = Sum[0]
modifyBSTUtil(root.left, Sum)
def modify(root):
modifyBSTUtil(root, [0])
return root | FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR LIST NUMBER RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
def modify(root):
Sum = [0]
modifyBSTUtil(root, Sum)
return root
def modifyBSTUtil(root, Sum):
if root == None:
return
modifyBSTUtil(root.right, Sum)
Sum[0] = Sum[0] + root.data
root.data = Sum[0]
modifyBSTUtil(root.left, Sum) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def ino(r, l):
if r is None:
return
ino(r.left, l)
l.append(r)
ino(r.right, l)
def modify(root):
l = []
ino(root, l)
n = len(l)
s = 0
for i in range(n - 1, -1, -1):
g = l[i].data
l[i].data += s
s += g
return root | FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
if not root:
return
def help(root, sum):
if not root:
return sum
sum = help(root.right, sum)
sum += root.data
root.data = sum
sum = help(root.left, sum)
return sum
help(root, 0)
return root | FUNC_DEF IF VAR RETURN FUNC_DEF IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def help(root, x):
if root is None:
return
help(root.right, x)
root.data = root.data + x[0]
x[0] = root.data
help(root.left, x)
def modify(root):
x = [0]
help(root, x)
return root | FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def inor(root):
s = []
ans = []
curr = root
while True:
if curr != None:
s.append(curr)
curr = curr.left
elif curr == None and s != []:
curr = s.pop()
ans.append(curr.data)
curr = curr.right
else:
break
return ans
def modify(root):
nodes = inor(root)
n = len(nodes)
for i in range(n - 2, -1, -1):
nodes[i] += nodes[i + 1]
i = 0
s = []
curr = root
while True:
if curr != None:
s.append(curr)
curr = curr.left
elif curr == None and s != []:
curr = s.pop()
curr.data = nodes[i]
i += 1
curr = curr.right
else:
break
return root | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
p = root
s = []
curr = 0
while len(s) > 0 or p:
if not p:
p = s[-1]
s.pop()
p.data += curr
curr = p.data
p = p.left
elif p:
s.append(p)
p = p.right
return root | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR IF VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
num = [0]
def fun(root, num):
if root == None:
return
fun(root.right, num)
root.data += num[0]
num[0] = root.data
fun(root.left, num)
fun(root, num)
return root | FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
def modify(root):
global sum
sum = 0
modifyUtil(root)
return root
def modifyUtil(root):
global sum
if root is None:
return
modifyUtil(root.right)
root.data += sum
sum = root.data
modifyUtil(root.left) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
total = 0
stack = []
n = root
while n or stack:
if not n:
n = stack.pop()
else:
while n.right:
stack.append(n)
n = n.right
total += n.data
n.data = total
n = n.left
return root | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
arr, dic1, sum1, q, pos, len1 = [], {}, 0, [root], 0, 1
while pos < len1:
arr.append(q[pos].data)
dic1[q[pos].data] = [0, q[pos]]
sum1 += q[pos].data
if q[pos].left:
q.append(q[pos].left)
len1 += 1
if q[pos].right:
q.append(q[pos].right)
len1 += 1
pos += 1
arr.sort()
dic1[arr[0]][0] = dic1[arr[0]][1].data = sum1
for i in range(1, len1):
dic1[arr[i]][0] = dic1[arr[i]][1].data = dic1[arr[i - 1]][0] - arr[i - 1]
return root | FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR LIST DICT NUMBER LIST VAR NUMBER NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR LIST NUMBER VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
def ino(root, A):
if root == None:
return [], 0
tot = 0
l, lt = ino(root.left, [])
A.extend(l)
tot += lt
A.append(root)
tot += root.data
r, rt = ino(root.right, [])
tot += rt
A.extend(r)
return A, tot
left = 0
arr, right = ino(root, [])
for node in arr:
prev = node.data
node.data = right - left
left += prev
return root | FUNC_DEF FUNC_DEF IF VAR NONE RETURN LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(r):
s = []
t = r
while t:
s.append(t)
t = t.right
t = 0
while s:
x = s.pop(-1)
x.data += t
t = x.data
if x.left:
s.append(x.left)
x = s[-1]
while x.right:
s.append(x.right)
x = x.right
return r | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | class Solution:
def __init__(self):
self.s = 0
def io(self, a):
if a == None:
return
self.io(a.right)
a.data += self.s
self.s = a.data
self.io(a.left)
def modify(root):
o = Solution()
o.io(root)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
store = [0]
modifyUtil(root, store)
return root
def modifyUtil(root, store):
if root.right:
modifyUtil(root.right, store)
x = root.data
root.data += store[0]
store[0] += x
if root.left:
modifyUtil(root.left, store) | FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
req = 0
stack = []
cur = root
while 1:
if cur:
stack.append(cur)
cur = cur.right
elif stack:
cur = stack.pop()
temp = cur.data
cur.data += req
req += temp
cur = cur.left
else:
break
return root | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def inorder1(root):
if not root:
return 0
nsum = 0
nsum += inorder1(root.left)
nsum += root.data
nsum += inorder1(root.right)
return nsum
def modify(root):
global nsum
nsum = inorder1(root)
def solve(root):
global nsum
if not root:
return
solve(root.left)
nsum -= root.data
root.data += nsum
solve(root.right)
solve(root)
return root | FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
ar = inor(root, [])
ar = ar[::-1]
su = ar[0]
arr = []
arr.append(su)
for i in range(1, len(ar)):
su = su + ar[i]
arr.append(su)
arr = arr[::-1]
ans = tree(0, len(arr) - 1, arr)
return ans
def inor(root, a):
if root == None:
return
inor(root.left, a)
a.append(root.data)
inor(root.right, a)
return a
def tree(s, e, arr):
if s > e:
return None
mid = (s + e) // 2
root = Node(arr[mid])
root.left = tree(s, mid - 1, arr)
root.right = tree(mid + 1, e, arr)
return root | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
def _solve(nd, pt):
if not nd:
return None
X = _solve(nd.right, pt)
if X is not None:
add = X
elif pt is not None:
add = pt.data
else:
add = 0
nd.data += add
Y = _solve(nd.left, nd)
return nd.data if Y is None else Y
_solve(root, None)
return root | FUNC_DEF FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR NONE RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
def f(root):
if root == None:
return 0
f(root.right)
summ[0] += root.data
root.data = summ[0]
f(root.left)
summ = []
summ.append(0)
f(root)
return root | FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
def traversal(root):
nonlocal sum1
if root == None:
return None
if root.right:
traversal(root.right)
sum1 += root.data
root.data = sum1
if root.left:
traversal(root.left)
sum1 = 0
traversal(root)
return root | FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def modify(root):
sumi = [0]
def ino(nod, flag):
if nod is None:
return
ino(nod.left, flag)
if flag == 0:
sumi[0] += nod.data
else:
k = nod.data
nod.data = sumi[0]
sumi[0] -= k
ino(nod.right, flag)
ino(root, 0)
ino(root, 1)
return root | FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a BST, modify it so that all greater values in the given BST are added to every node.
Example 1:
Input:
50
/ \
30 70
/ \ / \
20 40 60 80
Output: 350 330 300 260 210 150 80
Explanation:The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80
Example 2:
Input:
2
/ \
1 5
/ \
4 7
Output: 19 18 16 12 7
Your Task:
You don't need to read input or print anything. Your task is to complete the function modify() which takes one argument: root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1<=N<=100000
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code. | def inorder1(root, arr):
if not root:
return
inorder1(root.left, arr)
arr.append(root.data)
inorder1(root.right, arr)
return arr
def modify(root):
arr = inorder1(root, [])
global nsum
nsum = sum(arr)
def solve(root):
global nsum
if not root:
return
solve(root.left)
nsum -= root.data
root.data += nsum
solve(root.right)
solve(root)
return root | FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1], A{K+2], ... A[A.length - 1], A[0], A[1], ..., A[K-1]. Afterward, any entries that are less than or equal to their index are worth 1 point.
For example, if we have [2, 4, 1, 3, 0], and we rotate by K = 2, it becomes [1, 3, 0, 2, 4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point].
Over all possible rotations, return the rotation index K that corresponds to the highest score we could receive. If there are multiple answers, return the smallest such index K.
Example 1:
Input: [2, 3, 1, 4, 0]
Output: 3
Explanation:
Scores for each K are listed below:
K = 0, A = [2,3,1,4,0], score 2
K = 1, A = [3,1,4,0,2], score 3
K = 2, A = [1,4,0,2,3], score 3
K = 3, A = [4,0,2,3,1], score 4
K = 4, A = [0,2,3,1,4], score 3
So we should choose K = 3, which has the highest score.
Example 2:
Input: [1, 3, 0, 2, 4]
Output: 0
Explanation: A will always have 3 points no matter how it shifts.
So we will choose the smallest K, which is 0.
Note:
A will have length at most 20000.
A[i] will be in the range [0, A.length]. | class Solution:
def movesToChessboard(self, board):
n = len(board)
for i in range(1, n):
for j in range(1, n):
if (board[0][0] + board[0][j] + board[i][0] + board[i][j]) % 2 != 0:
return -1
result = 0
x = 0
y = 0
for i in range(0, n):
if i % 2 == 0 and board[i][0] == 1:
x += 1
elif i % 2 == 1 and board[i][0] == 0:
y += 1
if n % 2 == 0:
if x != y:
return -1
result += min(x, n // 2 - x)
elif x == y:
result += x
elif (n + 1) // 2 - x == (n - 1) // 2 - y:
result += (n + 1) // 2 - x
else:
return -1
x = 0
y = 0
for i in range(0, n):
if i % 2 == 0 and board[0][i] == 1:
x += 1
elif i % 2 == 1 and board[0][i] == 0:
y += 1
if n % 2 == 0:
if x != y:
return -1
result += min(x, n // 2 - x)
elif x == y:
result += x
elif (n + 1) // 2 - x == (n - 1) // 2 - y:
result += (n + 1) // 2 - x
else:
return -1
return result | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN NUMBER RETURN VAR |
Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1], A{K+2], ... A[A.length - 1], A[0], A[1], ..., A[K-1]. Afterward, any entries that are less than or equal to their index are worth 1 point.
For example, if we have [2, 4, 1, 3, 0], and we rotate by K = 2, it becomes [1, 3, 0, 2, 4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point].
Over all possible rotations, return the rotation index K that corresponds to the highest score we could receive. If there are multiple answers, return the smallest such index K.
Example 1:
Input: [2, 3, 1, 4, 0]
Output: 3
Explanation:
Scores for each K are listed below:
K = 0, A = [2,3,1,4,0], score 2
K = 1, A = [3,1,4,0,2], score 3
K = 2, A = [1,4,0,2,3], score 3
K = 3, A = [4,0,2,3,1], score 4
K = 4, A = [0,2,3,1,4], score 3
So we should choose K = 3, which has the highest score.
Example 2:
Input: [1, 3, 0, 2, 4]
Output: 0
Explanation: A will always have 3 points no matter how it shifts.
So we will choose the smallest K, which is 0.
Note:
A will have length at most 20000.
A[i] will be in the range [0, A.length]. | class Solution:
def movesToChessboard(self, board):
rows = [1]
for i in range(1, len(board)):
num = self.get_num(board[0], board[i])
if 0 <= num <= 1:
rows.append(num)
else:
return -1
r1 = self.swap_count(rows)
if r1 != -1:
r2 = self.swap_count(board[0])
if r1 == -1 or r2 == -1:
return -1
else:
return r1 + r2
def get_num(self, r1, r2):
eq = True
op = True
for i in range(len(r1)):
if r1[i] == r2[i]:
op = False
else:
eq = False
if eq:
return 1
elif op:
return 0
else:
return -1
def swap_count(self, bits):
n = len(bits)
ones = sum(bits)
zeros = n - ones
ones_in_even = 0
zeros_in_even = 0
for i in range(0, n, 2):
ones_in_even += bits[i]
zeros_in_even += 1 - bits[i]
if abs(ones - zeros) > 1:
return -1
if n % 2 == 0:
return min(zeros_in_even, ones_in_even)
elif ones > zeros:
return zeros_in_even
else:
return ones_in_even | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1], A{K+2], ... A[A.length - 1], A[0], A[1], ..., A[K-1]. Afterward, any entries that are less than or equal to their index are worth 1 point.
For example, if we have [2, 4, 1, 3, 0], and we rotate by K = 2, it becomes [1, 3, 0, 2, 4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point].
Over all possible rotations, return the rotation index K that corresponds to the highest score we could receive. If there are multiple answers, return the smallest such index K.
Example 1:
Input: [2, 3, 1, 4, 0]
Output: 3
Explanation:
Scores for each K are listed below:
K = 0, A = [2,3,1,4,0], score 2
K = 1, A = [3,1,4,0,2], score 3
K = 2, A = [1,4,0,2,3], score 3
K = 3, A = [4,0,2,3,1], score 4
K = 4, A = [0,2,3,1,4], score 3
So we should choose K = 3, which has the highest score.
Example 2:
Input: [1, 3, 0, 2, 4]
Output: 0
Explanation: A will always have 3 points no matter how it shifts.
So we will choose the smallest K, which is 0.
Note:
A will have length at most 20000.
A[i] will be in the range [0, A.length]. | class Solution:
def movesToChessboard(self, board):
N = len(board)
for i in range(N):
for j in range(N):
if board[0][0] ^ board[0][j] ^ board[i][0] ^ board[i][j]:
return -1
rowSum = 0
colSum = 0
rowSwap = 0
colSwap = 0
for i in range(N):
rowSum += board[0][i]
colSum += board[i][0]
rowSwap += board[0][i] == i % 2
colSwap += board[i][0] == i % 2
if rowSum < N // 2 or rowSum > (N + 1) // 2:
return -1
if colSum < N // 2 or colSum > (N + 1) // 2:
return -1
if N % 2:
if rowSwap % 2:
rowSwap = N - rowSwap
if colSwap % 2:
colSwap = N - colSwap
else:
rowSwap = min(rowSwap, N - rowSwap)
colSwap = min(colSwap, N - colSwap)
return (rowSwap + colSwap) // 2 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
hm = {}
hm[0] = 1
prefix = 0
count = 0
for i in range(0, len(A)):
prefix = (A[i] + prefix) % K
if prefix in hm:
count += hm[prefix]
else:
hm[prefix] = 0
hm[prefix] += 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
res = 0
sums = 0
d = {(0): 1}
for num in A:
sums = (sums + num) % K
res += d.get(sums, 0)
d[sums] = d.get(sums, 0) + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
mod_count = Counter({(0): 1})
ans = 0
for s in [*accumulate(A)]:
m = s % K
if m in mod_count:
ans += mod_count[m]
mod_count[m] += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
ans = 0
count = [1] + [0] * K
prefix = 0
for num in A:
prefix = (prefix + num) % K
ans += count[prefix]
count[prefix] += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
d = {(0): 1}
runningSum = 0
sol = 0
for i in range(len(A)):
runningSum += A[i]
newMod = runningSum % K
if newMod not in d:
d[newMod] = 0
d[newMod] += 1
sol += d[newMod] - 1
return sol | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
preSum = [0]
for num in A:
preSum.append((preSum[-1] + num) % K)
preSumCounter = Counter(preSum)
return sum(v * (v - 1) // 2 for v in preSumCounter.values()) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
m = Counter()
cur_sum = 0
ans = 0
for x in A:
cur_sum += x
ans += m[cur_sum % K]
if cur_sum % K == 0:
ans += 1
print((x, m[cur_sum % K]))
m[cur_sum % K] += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
sum1 = sum(A)
count1 = 0
dict1 = {(0): 1}
rem1 = 0
sum2 = 0
for i in range(len(A)):
sum2 += A[i]
rem1 = sum2 % K
if rem1 < 0:
rem1 += k
if rem1 in dict1:
count1 += dict1[rem1]
dict1[rem1] += 1
else:
dict1[rem1] = 1
print(dict1)
return count1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
D, s = {(0): 1}, 0
for a in A:
s = (s + a) % K
if s in D:
D[s] += 1
else:
D[s] = 1
return sum(i * (i - 1) // 2 for i in list(D.values())) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR DICT NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
hash_map = collections.defaultdict(int)
hash_map[0] = 1
prefix = 0
res = 0
for i in range(len(A)):
num = A[i]
prefix = (prefix + num) % K
res += hash_map[prefix]
hash_map[prefix] += 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
res = 0
currsum = 0
d = Counter()
d[0] = 1
for i, num in enumerate(A):
currsum += num
currsum %= K
if currsum in d:
res += d[currsum]
d[currsum] += 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
hashmap = [0] * K
hashmap[0] = 1
result = 0
prefix = 0
for i in range(len(A)):
prefix = (prefix + A[i]) % K
hashmap[prefix] += 1
result += hashmap[prefix] - 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
sums = 0
count = 0
dic = {(0): 1}
for a in A:
sums = (sums + a) % K
print(sums)
if sums in dic:
count += dic[sums]
dic[sums] += 1
else:
dic[sums] = 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
n = len(A)
prefix = [0] * (n + 1)
for i in range(1, n + 1):
prefix[i] = prefix[i - 1] + A[i - 1]
for i in range(1, n + 1):
prefix[i] = prefix[i] % K
d = defaultdict(list)
ans = 0
for i in range(1, n + 1):
if prefix[i] == 0:
ans += 1
if prefix[i] in d.keys():
ans += len(d[prefix[i]])
d[prefix[i]].append(i)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
res = 0
lookup = {(0): 1}
presum = 0
for i, num in enumerate(A):
presum += num
remainder = presum % K
res += lookup.get(remainder, 0)
lookup[remainder] = lookup.get(remainder, 0) + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
n = len(A)
A[0] = A[0] % K
seen = {(0): 1}
count = 0
for i in range(1, n):
A[i] = (A[i] + A[i - 1]) % K
for i in range(n):
newTarget = (A[i] - K) % K
if newTarget in seen:
count += seen[newTarget]
if A[i] in seen:
seen[A[i]] += 1
else:
seen[A[i]] = 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
cumsum = [0]
for n in A:
cumsum.append(cumsum[-1] + n)
print((cumsum, A))
cnt = 0
count = [0] * K
for x in cumsum:
count[(x % K + K) % K] += 1
cnt = 0
for num in count:
cnt += num * (num - 1) // 2
return cnt | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
prefix = [0] * len(A)
prefix[0] = A[0] % K
counter = Counter([prefix[0]])
ans = 1 if prefix[0] == 0 else 0
for i in range(1, len(A)):
prefix[i] = (prefix[i - 1] + A[i]) % K
target = prefix[i] - 0
ans += counter[target]
if target == 0:
ans += 1
counter[prefix[i]] += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
P = [0]
for x in A:
P.append((P[-1] + x) % K)
count = collections.Counter(P)
ans = 0
for v in list(count.values()):
ans += int(v * (v - 1) * 0.5)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
res = 0
cs = 0
seen = collections.Counter({(0): 1})
for i in range(len(A)):
x = A[i]
cs += x
if cs % K in seen:
res += seen[cs % K]
seen[cs % K] += 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
su = 0
dic = {}
count = 0
for i in range(len(A)):
su += A[i]
if su % K == 0:
count += 1
if su % K in dic.keys():
count += dic[su % K]
if su % K in dic.keys():
dic[su % K] += 1
else:
dic[su % K] = 1
print(dic)
print(count)
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
countPrefs = {(0): 1}
total = 0
res = 0
for i, num in enumerate(A):
total += num
res += countPrefs.get(total % K, 0)
if total % K in countPrefs:
countPrefs[total % K] += 1
else:
countPrefs[total % K] = 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
prefix_sum = [0]
for idx, i in enumerate(A):
prefix_sum.append(i + prefix_sum[idx])
print(prefix_sum)
num_enc = dict()
total = 0
for i in prefix_sum:
if i % K in num_enc:
total += num_enc[i % K]
if i % K - K in num_enc:
total += num_enc[i % K - K]
if i % K not in num_enc:
num_enc[i % K] = 0
num_enc[i % K] += 1
return total | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
B = [(a % K) for a in A]
sum_dict = {}
ans = 0
curr_sum = 0
sum_dict[0] = 1
n = len(B)
for i, num in enumerate(B):
curr_sum = (curr_sum + num) % K
if curr_sum in sum_dict:
ans += sum_dict[curr_sum]
sum_dict[curr_sum] = sum_dict.get(curr_sum, 0) + 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution(object):
def subarraysDivByK(self, A, K):
nums = A
k = K
modDict = {}
tempSumn = 0
ans = 0
continousSum = []
for num in nums:
tempSumn += num
continousSum.append(tempSumn)
remain = tempSumn % k
if remain not in modDict:
modDict[remain] = 0
modDict[remain] += 1
diff = k
for i in range(0, len(continousSum)):
if diff % k in modDict:
ans += modDict[diff % k]
diff = continousSum[i]
modDict[diff % k] -= 1
return ans | CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
if not A or not K:
return 0
map = Counter()
map[0] = 1
sum = 0
res = 0
for num in A:
sum += num
sum %= K
if sum < 0:
sum += K
if sum in map:
res += map[sum]
map[sum] += 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
prefixMod = 0
hashTable = collections.defaultdict(int)
total = 0
curSum = 0
hashTable[0] = 1
for i, x in enumerate(A):
prefixMod = prefixMod + x
prefixMod = prefixMod % K
total += hashTable[prefixMod]
hashTable[prefixMod] += 1
return total | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
dp = [1] + [0] * K
result = 0
running_sum = 0
for num in A:
running_sum += num
result += dp[running_sum % K]
dp[running_sum % K] += 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
n = len(A)
prefixsum = 0
remain = collections.defaultdict(int)
remain[0] = 1
res = 0
for i in range(n):
prefixsum += A[i]
re = prefixsum % K
res += remain[re]
remain[re] += 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
map = {}
map[0] = 1
sum, count = 0, 0
for n in A:
sum = (sum + n) % K
if sum not in list(map.keys()):
map[sum] = 1
else:
count += map[sum]
map[sum] += 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
m = defaultdict(int)
m[0] = 1
curr_sum = 0
result = 0
for a in A:
curr_sum += a
remainder = curr_sum % K
if remainder in m:
result += m[remainder]
m[remainder] += 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def TRIAlsubarraysDivByK(self, A: List[int], K: int) -> int:
n = 0
for i in range(len(A)):
sum = 0
for j in range(i, len(A)):
sum += A[j]
if sum % K == 0:
n += 1
return n
def subarraysDivByK(self, A: List[int], K: int) -> int:
rems = [0] * len(A)
for i in range(len(A)):
if i == 0:
rems[0] = A[0] % K
else:
rems[i] = (rems[i - 1] + A[i]) % K
print(rems)
count = list(Counter(rems).values())
print(count)
return int(sum(n * (n - 1) / 2 for n in count)) + rems.count(0) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
cnt = Counter([0])
pref, ans = 0, 0
for a in A:
pref = (pref + a) % K
ans += cnt[pref]
cnt[pref] += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
1 <= A.length <= 30000
-10000 <= A[i] <= 10000
2 <= K <= 10000 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
res, n, cache, sv = 0, len(A), collections.defaultdict(int), 0
cache[0] = 1
for i, v in enumerate(A):
sv += v
if cache[sv % K] > 0:
res += cache[sv % K]
cache[sv % K] += 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def inorder(self, root, prev, first, second):
if root.left != None:
prev, first, second = self.inorder(root.left, prev, first, second)
if prev != None:
if prev.data > root.data:
if first == None:
first = prev
second = root
prev = root
if root.right != None:
prev, first, second = self.inorder(root.right, prev, first, second)
return prev, first, second
def correctBST(self, root):
prev, first, second = self.inorder(root, prev=None, first=None, second=None)
first.data, second.data = second.data, first.data
return root | CLASS_DEF FUNC_DEF IF VAR NONE ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NONE NONE NONE ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBSTUtil(self, root, first, middle, last, prev):
if root:
self.correctBSTUtil(root.left, first, middle, last, prev)
if prev[0] and root.data < prev[0].data:
if not first[0]:
first[0] = prev[0]
middle[0] = root
else:
last[0] = root
prev[0] = root
self.correctBSTUtil(root.right, first, middle, last, prev)
def correctBST(self, root):
first = [None]
middle = [None]
last = [None]
prev = [None]
self.correctBSTUtil(root, first, middle, last, prev)
if first[0] and last[0]:
first[0].data, last[0].data = last[0].data, first[0].data
elif first[0] and middle[0]:
first[0].data, middle[0].data = middle[0].data, first[0].data
return root | CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def inorder(self, root, arr):
if root is None:
return
self.inorder(root.left, arr)
arr.append(root.data)
self.inorder(root.right, arr)
def update(self, root, arr):
if root is None:
return
self.update(root.left, arr)
root.data = arr.pop(-1)
self.update(root.right, arr)
def correctBST(self, root):
arr = []
self.inorder(root, arr)
arr.sort(reverse=True)
self.update(root, arr)
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def __init__(self):
self.first = None
self.middle = None
self.second = None
def check(self, prev, curr):
if prev and prev.data > curr.data:
if self.first == None:
self.first = prev
self.middle = curr
elif self.second == None:
self.second = curr
def correctBST(self, root):
root1 = root
prev = None
curr = root
while curr:
node = curr.left
if node == None:
self.check(prev, curr)
prev = curr
curr = curr.right
else:
while node.right and node.right != curr:
node = node.right
if node.right == None:
node.right = curr
curr = curr.left
else:
node.right = None
self.check(prev, curr)
prev = curr
curr = curr.right
if self.first and self.second:
temp = self.first.data
self.first.data = self.second.data
self.second.data = temp
else:
temp = self.first.data
self.first.data = self.middle.data
self.middle.data = temp
return root1 | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
first, second = None, None
prev = None
def solves(root):
if root == None:
return
nonlocal first, second, prev
solves(root.left)
if prev != None and prev.data > root.data:
if first == None:
first = prev
second = root
prev = root
solves(root.right)
solves(root)
first.data, second.data = second.data, first.data
return root | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NONE NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | import sys
sys.setrecursionlimit(10**6)
class Solution:
def correctBST(self, root):
self.first, self.middle, self.last, self.prev = None, None, None, None
def inorder(root):
if root is None:
return
inorder(root.left)
if self.prev is not None and self.prev.data > root.data:
if self.first is not None:
self.last = root
else:
self.first, self.middle = self.prev, root
self.prev = root
inorder(root.right)
inorder(root)
if self.last is None:
self.first.data, self.middle.data = (self.middle.data, self.first.data)
else:
self.first.data, self.last.data = self.last.data, self.first.data
return root | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NONE NONE NONE NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
elements = []
index = [0]
def getEle(node):
if node is None:
return
getEle(node.left)
elements.append(node.data)
getEle(node.right)
temp = root
getEle(temp)
elements.sort()
def setEle(node):
if node is None:
return
setEle(node.left)
node.data = elements[index[0]]
index[0] += 1
setEle(node.right)
temp = root
setEle(temp)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
self.inorder = []
self.inOrder(root)
the_two = []
for i in range(len(self.inorder) - 1):
if self.inorder[i].data > self.inorder[i + 1].data:
the_two.append(i)
if len(the_two) == 1:
f, s = self.inorder[the_two[0]], self.inorder[the_two[0] + 1]
f.data, s.data = s.data, f.data
else:
f, s = self.inorder[the_two[0]], self.inorder[the_two[1] + 1]
f.data, s.data = s.data, f.data
return root
def inOrder(self, r):
if r:
self.inOrder(r.left)
self.inorder.append(r)
self.inOrder(r.right) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Node:
def __init__(self, val):
self.right = None
self.data = val
self.left = None
class Solution:
def __init__(self):
self.prev = None
self.first = None
self.second = None
def inorder(self, root):
if root is None:
return
self.inorder(root.left)
if self.prev and self.prev.data > root.data:
if not self.first:
self.first = self.prev
self.second = root
self.prev = root
self.inorder(root.right)
def correctBST(self, root):
self.inorder(root)
self.first.data, self.second.data = self.second.data, self.first.data
return root | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | def inorder_traversal(root):
if root is None:
return []
return inorder_traversal(root.left) + [root] + inorder_traversal(root.right)
class Solution:
def correctBST(self, root):
nodes = inorder_traversal(root)
first = None
second = None
prev = nodes[0]
for node in nodes[1:]:
if node.data < prev.data:
if first is None:
first = prev
second = node
prev = node
first.data, second.data = second.data, first.data
return root | FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
def _solve(nd):
if not nd:
return
nonlocal first, prev
if _solve(nd.left):
return True
if not first:
if prev and prev.data > nd.data:
first = prev
elif first.data < nd.data:
first.data, prev.data = prev.data, first.data
return True
prev = nd
if _solve(nd.right):
return True
return False
first, prev = None, None
if not _solve(root):
first.data, prev.data = prev.data, first.data
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN IF FUNC_CALL VAR VAR RETURN NUMBER IF VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NONE NONE IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def visit(self, root, data):
if root is None:
return
self.visit(root.left, data)
data.append(root.data)
self.visit(root.right, data)
return
def fix(self, root, idxes, values, precnts=0):
if root is None:
return 0
num1 = self.fix(root.left, idxes, values, precnts)
if precnts + num1 == idxes[0]:
root.data = values[-1]
if precnts + num1 == idxes[-1]:
root.data = values[0]
num2 = self.fix(root.right, idxes, values, precnts + num1 + 1)
return num1 + 1 + num2
def correctBST(self, root):
if root is None:
return
data = []
self.visit(root, data)
idxes = []
values = []
for i in range(len(data)):
if i == 0:
if data[i] > data[i + 1]:
idxes.append(i)
values.append(data[i])
elif i == len(data) - 1:
if data[i] < data[i - 1]:
idxes.append(i)
values.append(data[i])
elif (data[i] - data[i - 1]) * (data[i] - data[i + 1]) > 0:
idxes.append(i)
values.append(data[i])
self.fix(root, idxes, values)
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
self.f = None
self.m = None
self.l = None
self.prev = None
self.inorder(root)
if root == self.f:
root = self.f
if self.l == None:
self.f.data, self.m.data = self.m.data, self.f.data
else:
self.f.data, self.l.data = self.l.data, self.f.data
return root
def inorder(self, root):
if not root:
return
self.inorder(root.left)
if self.prev and root.data < self.prev.data:
if self.f == None:
self.f = self.prev
self.m = root
else:
self.l = root
self.prev = root
self.inorder(root.right) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | import sys
class Solution:
def correctBST(self, root):
def inorder(root):
if not root:
return
inorder(root.left)
arr.append(root.data)
inorder(root.right)
def update(node, arr):
if not node:
return
update(node.left, arr)
node.data = arr.pop(len(arr) - 1)
update(node.right, arr)
arr = []
inorder(root)
arr = sorted(arr, reverse=True)
update(root, arr)
return root | IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
self.prev = None
self.first = None
self.second = None
self.find_swapped_nodes(root)
self.swap_nodes(self.first, self.second)
return root
def find_swapped_nodes(self, root):
if root is None:
return
self.find_swapped_nodes(root.left)
if self.prev is not None and root.data < self.prev.data:
if self.first is None:
self.first = self.prev
self.second = root
self.prev = root
self.find_swapped_nodes(root.right)
def swap_nodes(self, node1, node2):
temp = node1.data
node1.data = node2.data
node2.data = temp | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | def inOrderRead(root, arr, nodes):
if root == None:
return
if root.left:
inOrderRead(root.left, arr, nodes)
arr.append(root.data)
nodes.append(root)
if root.right:
inOrderRead(root.right, arr, nodes)
class Solution:
def correctBST(self, root):
badNodeL = None
badNodeR = None
arr = []
nodes = []
inOrderRead(root, arr, nodes)
if arr[0] != min(arr):
badNodeL = nodes[0]
if arr[-1] != max(arr):
badNodeR = nodes[-1]
for n in range(1, len(arr) - 1):
if not badNodeL:
if arr[n] > arr[n + 1]:
badNodeL = nodes[n]
for n in range(len(arr) - 1, 0, -1):
if not badNodeR:
if arr[n] < arr[n - 1]:
badNodeR = nodes[n]
temp = badNodeR.data
badNodeR.data = badNodeL.data
badNodeL.data = temp
return root | FUNC_DEF IF VAR NONE RETURN IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def traverse(self, root, arr):
if root == None:
return
self.traverse(root.left, arr)
arr.append(root.data)
self.traverse(root.right, arr)
def change(self, root, a, b, rt):
if root == None:
return
if root.data == a:
rt.append(root)
if root.data == b:
rt.append(root)
self.change(root.left, a, b, rt)
self.change(root.right, a, b, rt)
def correctBST(self, root):
arr = []
self.traverse(root, arr)
a = []
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
a.append(arr[i])
break
for i in range(len(arr) - 1, 0, -1):
if arr[i - 1] > arr[i]:
a.append(arr[i])
break
if len(a) == 2:
rt = []
self.change(root, a[0], a[1], rt)
rt[0].data, rt[1].data = rt[1].data, rt[0].data
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR 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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
l = []
global prev
prev = [-(2**31) - 2, -(2**31) - 2]
def fun(node):
global prev
if node is None:
return
fun(node.left)
if node.data < prev[0] and not l:
l.append(prev[1])
l.append(node)
f = 1
elif node.data < prev[0] and l:
l.pop()
l.append(node)
prev = [node.data, node]
fun(node.right)
fun(root)
l[0].data, l[1].data = l[1].data, l[0].data
return root | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
def inorderTraversal(node):
nonlocal prev, first, middle, last
if node:
inorderTraversal(node.left)
if prev and node.data < prev.data:
if not first:
first = prev
middle = node
prev = node
inorderTraversal(node.right)
prev = None
first = None
middle = None
last = None
inorderTraversal(root)
if first and last:
first.data, last.data = last.data, first.data
elif first and middle:
first.data, middle.data = middle.data, first.data
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def __init__(self):
self.first = None
self.last = None
self.prev = None
def swap(self, prev, curr):
tmp = prev.data
prev.data = curr.data
curr.data = tmp
def rec(self, root):
if root is not None:
self.rec(root.left)
if self.prev is not None and self.prev.data > root.data:
if self.first is None:
self.first = self.prev
self.last = root
else:
self.last = root
self.prev = root
self.rec(root.right)
def correctBST(self, root):
self.rec(root)
if self.first is not None and self.last is not None:
self.swap(self.first, self.last)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
first, second, prev = None, None, Node(float("-inf"))
def inorder_traversal(node):
nonlocal first, second, prev
if not node:
return
inorder_traversal(node.left)
if not first and prev.data > node.data:
first = prev
if first and prev.data > node.data:
second = node
prev = node
inorder_traversal(node.right)
inorder_traversal(root)
first.data, second.data = second.data, first.data
return root | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NONE NONE FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
self.first = None
self.second = None
self.prev = None
self.inorder(root)
self.first.data, self.second.data = self.second.data, self.first.data
return root
def inorder(self, node):
if node.left:
self.inorder(node.left)
if self.prev and node.data < self.prev.data:
if self.first == None:
self.first = self.prev
self.second = node
else:
self.second = node
self.prev = node
if node.right:
self.inorder(node.right) | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
i = 0
def inorder(self, root, l):
if root == None:
return []
self.inorder(root.left, l)
l.append(root.data)
self.inorder(root.right, l)
def solve(self, root, l):
if root == None:
return None
self.solve(root.left, l)
root.data = l[self.i]
self.i += 1
self.solve(root.right, l)
def correct(self, l):
f, h = None, None
for i in range(len(l) - 1):
if l[i] > l[i + 1]:
if f == None:
f = i
else:
h = i + 1
break
if h == None:
h = f + 1
l[f], l[h] = l[h], l[f]
def correctBST(self, root):
l = []
self.inorder(root, l)
self.correct(l)
self.solve(root, l)
return root | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NONE NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
def find_wrong(root):
nonlocal x, y, prev
if root is None:
return
find_wrong(root.left)
if prev and root.data < prev.data:
y = root
if x is None:
x = prev
else:
return
prev = root
find_wrong(root.right)
x = y = prev = None
find_wrong(root)
x.data, y.data = y.data, x.data
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | import sys
class Solution:
def correctBST(self, root):
def inorder(root):
if root == None:
return
inorder(root.left)
if prev[0] != None and root.data < prev[0].data:
if first[0] == None:
first[0] = prev[0]
middle[0] = root
else:
last[0] = root
prev[0] = root
inorder(root.right)
first = [None]
last = [None]
middle = [None]
prev = [None]
inorder(root)
if first[0] != None and last[0] != None:
first[0].data, last[0].data = last[0].data, first[0].data
elif first[0] != None and middle[0] != None:
first[0].data, middle[0].data = middle[0].data, first[0].data
return root | IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE VAR VAR NUMBER IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR IF VAR NUMBER NONE VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NONE VAR NUMBER NONE ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
self.arr = []
self.index = 0
self.cus(root)
self.arr.sort()
self.srt(root)
return root
def cus(self, root):
if root is None:
return
self.cus(root.left)
self.arr.append(root.data)
self.cus(root.right)
def srt(self, root):
if root is None:
return
self.srt(root.left)
root.data = self.arr[self.index]
self.index += 1
self.srt(root.right) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def getBSTNode(self, root, arr):
if root == None:
return
self.getBSTNode(root.left, arr)
arr.append(root)
self.getBSTNode(root.right, arr)
def correctBST(self, root):
arr = []
self.getBSTNode(root, arr)
idx1 = -1
idx2 = -1
for i in range(0, len(arr) - 1):
if arr[i].data > arr[i + 1].data:
if idx1 == -1:
idx1 = i
else:
idx2 = i + 1
if idx2 == -1:
idx2 = idx1 + 1
tmp = arr[idx1].data
arr[idx1].data = arr[idx2].data
arr[idx2].data = tmp
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def correctBST(self, root):
self.first = None
self.second = None
self.prev = None
def inorder(node=root):
if not node:
return False
if inorder(node.left):
return True
if self.prev:
if node.data < self.prev.data and not self.first:
self.first = self.prev
self.second = node
elif node.data < self.prev.data and self.first:
self.second = node
return True
self.prev = node
if inorder(node.right):
return True
inorder()
t = self.first.data
self.first.data = self.second.data
self.second.data = t
return root | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER IF VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST by swapping them back. Do not change the structure of the tree.
Note: It is guaranteed that the given input will form BST, except for 2 nodes that will be wrong.
Example 1:
Input:
10
/ \
5 8
/ \
2 20
Output: 1
Explanation:
Example 2:
Input:
11
/ \
3 17
\ /
4 10
Output: 1
Explanation:
By swapping nodes 11 and 10, the BST
can be fixed.
Your Task:
You don't need to take any input. Just complete the function correctBst() that takes root node as parameter. The function should return the root of corrected BST. BST will then be checked by driver code and 0 or 1 will be printed.
Expected Time Complexity: O(Number of nodes)
Expected Auxiliary Space: O(logN), N = number of nodes
Constraints:
1 <= Number of nodes <= 10^5 | class Solution:
def __init__(self):
self.ans = []
self.d = {}
def correctBST(self, root):
self.inorder(root)
temp = sorted(self.ans)
for i in range(len(self.ans)):
if self.ans[i] != temp[i]:
r1 = self.d[self.ans[i]]
r2 = self.d[temp[i]]
x = r1.data
r1.data = r2.data
r2.data = x
return root
return root
def inorder(self, root):
if root is None:
return
self.d[root.data] = root
self.inorder(root.left)
self.ans.append(root.data)
self.inorder(root.right)
return | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.