description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): if root.left == None and root.right == None: return root.data elif root.right == None: return root.data * self.findMaxScore(root.left) elif root.left == None: return root.data * self.findMaxScore(root.right) else: return max( root.data * self.findMaxScore(root.left), root.data * self.findMaxScore(root.right), )
CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
def recur(root): if root.left or root.right: n1 = -99999 n2 = -99999 if root.left: n1 = recur(root.left) * root.data if root.right: n2 = recur(root.right) * root.data return max(n1, n2) else: return root.data class Solution: def findMaxScore(self, root): return recur(root)
FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def dfs(self, root, prod, res): if root is None: return res prod = prod * root.data res = max(prod, res) left_max = self.dfs(root.left, prod, res) right_max = self.dfs(root.right, prod, res) return max(res, max(left_max, right_max)) def findMaxScore(self, root): return self.dfs(root, 1, 1)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER NUMBER
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: l = 1 m = [] max = 0 def findMaxScore(self, root): if root == None: return 1 left = root.data * self.findMaxScore(root.left) right = root.data * self.findMaxScore(root.right) return max(left, right)
CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): max_score = self.traverse(root, 0) return max_score def traverse(self, node, score): if node is None: return 1 left = self.traverse(node.left, score) right = self.traverse(node.right, score) return node.data * max(left, right)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): if root is None: return 1 lh = root.data * self.findMaxScore(root.left) rh = root.data * self.findMaxScore(root.right) if lh > rh: return lh else: return rh
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def dfs(self, n, p): if n == None: self.max_p = max(p, self.max_p) else: self.dfs(n.left, p * n.data) self.dfs(n.right, p * n.data) def findMaxScore(self, root): self.max_p = 1 self.dfs(root, 1) return self.max_p
CLASS_DEF FUNC_DEF IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): ans = [1, 1] self.preorder(root, ans) return ans[0] def preorder(self, root, ans): if root == None: return if root.left == root.right == None: ans[0] = max(ans[0], ans[1] * root.data) return ans[1] = ans[1] * root.data self.preorder(root.left, ans) self.preorder(root.right, ans) ans[1] = ans[1] // root.data
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def tra(root): if root: return max(tra(root.left), tra(root.right)) * root.data else: return 1 return tra(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def answer(root): if root == None: return 1 l = answer(root.left) r = answer(root.right) return max(l * root.data, r * root.data) return answer(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def mx(r): if not r: return 1 if not r.left and not r.right: return r.data return r.data * max(mx(r.left), mx(r.right)) return mx(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): self.x = 1 self.maxer = -100000000000 def paths(root): if root is None: return self.x = self.x * root.data if root.left is None and root.right is None: if self.x > self.maxer: self.maxer = self.x paths(root.left) paths(root.right) self.x = int(self.x // root.data) paths(root) return self.maxer
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN ASSIGN VAR BIN_OP VAR VAR IF VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def find(root): if root is None: return 1 return max(root.data * find(root.left), root.data * find(root.right)) return find(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def f(root, temp, ans): if not root: ans[0] = max(ans[0], temp) else: temp *= root.data f(root.left, temp, ans) f(root.right, temp, ans) ans = [1] f(root, 1, ans) return ans[0]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR NUMBER
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): if root is None: return 1 else: if root.left is not None: lScore = self.findMaxScore(root.left) else: lScore = 1 if root.right is not None: rScore = self.findMaxScore(root.right) else: rScore = 1 return max(root.data * lScore, root.data * rScore)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def f(root, maxi, ans): if root.left == None and root.right == None: maxi[0] = max(maxi[0], ans) return if root.left: f(root.left, maxi, ans * root.left.data) if root.right: f(root.right, maxi, ans * root.right.data) maxi = [-100000000] ans = root.data f(root, maxi, ans) return maxi[0]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE VAR NONE ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): c = 0 q = [[root, root.data]] m = -1 while c != len(q): t = q[c][0] g = q[c][1] f = 0 if t.left: f = 1 q.append([t.left, g * t.left.data]) if t.right: f = 1 q.append([t.right, g * t.right.data]) c += 1 if f == 0: if g > m: m = g return m
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def MaxScore(self, root, score=0, high=0): if score == 0: score = root.data else: score *= root.data if root.left or root.right: if root.left: z = self.MaxScore(root.left, score, high) if z > high: high = z if root.right: z = self.MaxScore(root.right, score, high) if z > high: high = z score /= root.data else: if score > high: high = score score /= root.data return high def findMaxScore(self, root): a = self.MaxScore(root) return a
CLASS_DEF FUNC_DEF NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): lis = [] val = 1 def rec(root, val): val = val * root.data if root.left == None and root.right == None: lis.append(val) if root.right: rec(root.right, val) if root.left: rec(root.left, val) rec(root, val) return max(lis)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def __init__(self): self.m = 0 def findMaxScore(self, root): self.solve(root, 1) return self.m def solve(self, root, p): if root == None: return if root.left == None and root.right == None: self.m = max(self.m, p * root.data) return self.solve(root.left, p * root.data) self.solve(root.right, p * root.data)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): return self.max_score(root, 1) def max_score(self, root, current_score): if root is None: return current_score update_score = current_score * root.data return max( self.max_score(root.left, update_score), self.max_score(root.right, update_score), )
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def recur(root, sm): if not root: return if not root.left and not root.right: self.mx = max(self.mx, sm * root.data) return recur(root.left, sm * root.data) recur(root.right, sm * root.data) self.mx = float("-inf") recur(root, 1) return self.mx
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): q = [] q.append(root) while q != []: cur = q.pop(0) if cur.left != None: q.append(cur.left) cur.left.data = cur.left.data * cur.data if cur.right != None: q.append(cur.right) cur.right.data = cur.right.data * cur.data ma = 0 z = [] z.append(root) while z != []: cur = z.pop(0) ma = max(ma, cur.data) if cur.left != None: z.append(cur.left) if cur.right != None: z.append(cur.right) return ma
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def f(root): if not root: return 1 l = f(root.left) r = f(root.right) return root.data * max(l, r) return f(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def solve(root, maxi, psf): if not root: return 1 psf *= root.data if psf > maxi[0]: maxi[0] = psf solve(root.left, maxi, psf) solve(root.right, maxi, psf) maxi = [float("-inf")] solve(root, maxi, 1) return maxi[0]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): maxScore = [0] self.findMaxScoreAux(root, maxScore, 1) return maxScore[0] def findMaxScoreAux(self, root, maxScore, scoreSoFar): scoreSoFar *= root.data if root.left is None and root.right is None: if scoreSoFar > maxScore[0]: maxScore[0] = scoreSoFar if root.left is not None: self.findMaxScoreAux(root.left, maxScore, scoreSoFar) if root.right is not None: self.findMaxScoreAux(root.right, maxScore, scoreSoFar)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF VAR VAR IF VAR NONE VAR NONE IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): score_right = score_left = 1 if root.right is not None: score_right = self.findMaxScore(root.right) if root.left is not None: score_left = self.findMaxScore(root.left) return root.data * max(score_right, score_left)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): t = 1 def util(root): t = 1 if root == None: return if root.left == None and root.right == None: return root.data if root.left == None: return root.data * util(root.right) if root.right == None: return root.data * util(root.left) t *= root.data * max(util(root.left), util(root.right)) return t return util(root)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE RETURN IF VAR NONE VAR NONE RETURN VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR NONE RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): if not root: return 1 return root.data * max( self.findMaxScore(root.left), self.findMaxScore(root.right) )
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def helper(self, root, l, m): if root == None: return None l = l * root.data if root.left == None and root.right == None: m.append(l) l = 1 self.helper(root.left, l, m) self.helper(root.right, l, m) def findMaxScore(self, root): l = 1 m = [] max = 0 self.helper(root, l, m) for i in m: if i > max: max = i return max
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR BIN_OP VAR VAR IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def findMaxProduct(root, product): product[0] *= root.data if root.left: findMaxProduct(root.left, product) if root.right: findMaxProduct(root.right, product) if not root.left and not root.right: self.max_prod = max(self.max_prod, product[0]) product[0] = product[0] // root.data self.max_prod = 0 findMaxProduct(root, [1]) return self.max_prod
CLASS_DEF FUNC_DEF FUNC_DEF VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR LIST NUMBER RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def traversal(root): nonlocal product, maximum if root == None: return None product = product * root.data if root.left == None and root.right == None: maximum = max(maximum, product) if traversal(root.left) or traversal(root.right): return True product = product / root.data return False product = 1 maximum = -999999 traversal(root) return int(maximum)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR BIN_OP VAR VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): s = 1 l = [] self.dfs(root, s, l) return max(l) def dfs(self, root, s, l): if root == None: return s = s * root.data l.append(s) self.dfs(root.left, s, l) self.dfs(root.right, s, l)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def __init__(self): self.prod = [] def pre(self, root, p): if root.left == None and root.right == None: self.prod.append(p * root.data) return if root.left != None: self.pre(root.left, p * root.data) if root.right != None: self.pre(root.right, p * root.data) def findMaxScore(self, root): self.pre(root, 1) return max(self.prod)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE VAR NONE EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def getans(root): if root is None: return 0 leftbranch = getans(root.left) rightbranch = getans(root.right) branchmax = max(rightbranch, leftbranch) value = root.data summax = max(value * branchmax, value) return summax ans = getans(root) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): maxi = [-999999999999999] def solve(root, ans): if root.left is None and root.right is None: ans = ans * root.data maxi[0] = max(maxi[0], ans) return if root.left: solve(root.left, ans * root.data) if root.right: solve(root.right, ans * root.data) solve(root, 1) return maxi[0]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE VAR NONE ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): self.ans = 1 def helper(root): if root == None: return 1 l = helper(root.left) r = helper(root.right) temp = root.data * max(l, r) self.ans = max(self.ans, temp) return temp helper(root) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): def helper(root): if root == None: return 1 return root.data * max(helper(root.left), helper(root.right)) ans = helper(root) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def solve(self, p, curr): if not p: return curr *= p.data if not p.left and not p.right: self.ans = max(self.ans, curr) self.solve(p.left, curr) self.solve(p.right, curr) def findMaxScore(self, root): self.ans = 0 self.solve(root, 1) return self.ans
CLASS_DEF FUNC_DEF IF VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
In an online game, N blocks are arranged in a hierarchical manner. All the blocks are connected together by a total of N-1 connections. Each block is given an ID from 1 to N. A block may be further connected to other blocks. Each block is also assigned a specific point value. A player starts from Block 1. She must move forward from each block to a directly connected block until she reaches the final block, which has no further connection. When the player lands on a block, she collects the point value of that block. The players score is calculated as the product of point values that the player collects. Find the maximum score a player can achieve. Note: The answer can always be represented with 64 bits. Example 1: Input: 4 / \ 2 8 / \ / \ 2 1 3 4 Output: 128 Explanation: Path in the given tree goes like 4, 8, 4 which gives the max score of 4x8x4 = 128. Example 2: Input: 10 / \ 7 5 \ 1 Output: 70 Explanation: The path 10, 7 gives a score of 70 which is the maximum possible. Your Task: You don't need to take input or print anything. Your task is to complete the function findMaxScore() that takes root as input and returns max score possible. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the Tree). Constraints: 1 ≤ Number of nodes ≤ 10^{3} 1 ≤ Data on node ≤ 10 It is guaranteed that the answer will always fit in the 64-bit Integer
class Solution: def findMaxScore(self, root): self.prod = 1 def dfs(root, s): if root: dfs(root.left, s * root.data) self.prod = max(self.prod, s * root.data) dfs(root.right, s * root.data) return self.prod return dfs(root, 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val. Example: Given nums = [1, 3, 5] sumRange(0, 2) -> 9 update(1, 2) sumRange(0, 2) -> 8 Note: The array is only modifiable by the update function. You may assume the number of calls to update and sumRange function is distributed evenly.
class NumArray: def __init__(self, nums): self.segTree = [(0) for _ in range(len(nums) * 2)] self.build(nums) self.n = len(nums) def build(self, nums): n = len(nums) self.segTree[n : 2 * n] = nums[:] for i in range(n - 1, 0, -1): self.segTree[i] = self.eval(self.segTree[2 * i], self.segTree[2 * i + 1]) def eval(self, x1, x2): return x1 + x2 def update(self, i, val): k = i + self.n delta = val - self.segTree[k] while k > 0: self.segTree[k] += delta k //= 2 def sumRange(self, i, j): l = i + self.n r = j + self.n s = 0 while l <= r: if l & 1 == 1: s += self.segTree[l] l += 1 if r & 1 == 0: s += self.segTree[r] r -= 1 l >>= 1 r >>= 1 return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val. Example: Given nums = [1, 3, 5] sumRange(0, 2) -> 9 update(1, 2) sumRange(0, 2) -> 8 Note: The array is only modifiable by the update function. You may assume the number of calls to update and sumRange function is distributed evenly.
class NumArray: def __init__(self, nums): self.nums = nums self.step = 20 self.cache = [0] * math.ceil(len(nums) / self.step) for i in range(len(nums)): self.cache[i // self.step] += nums[i] def update(self, i, val): old = self.nums[i] self.nums[i] = val self.cache[i // self.step] += val - old def sumRange(self, i, j): if j // self.step - i // self.step > 1: spliti = (i // self.step + 1) * self.step - 1 splitj = j // self.step * self.step sumi = sum(self.nums[i : spliti + 1]) sumj = sum(self.nums[splitj : j + 1]) sumbetween = sum(self.cache[i // self.step + 1 : j // self.step]) return sumi + sumj + sumbetween else: return sum(self.nums[i : j + 1])
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val. Example: Given nums = [1, 3, 5] sumRange(0, 2) -> 9 update(1, 2) sumRange(0, 2) -> 8 Note: The array is only modifiable by the update function. You may assume the number of calls to update and sumRange function is distributed evenly.
class NumArray: def __init__(self, nums): l = len(nums) self.nums = nums self.bit = [0] * (l + 1) for i in range(1, l + 1): self.update_delta(i, nums[i - 1]) def update(self, i, val): bit = self.bit delta = val - self.nums[i] self.nums[i] = val self.update_delta(i + 1, delta) def sumRange(self, i, j): return self.get_sum(j + 1) - self.get_sum(i) def get_sum(self, i): bit = self.bit res = 0 while i > 0: res += bit[i] i -= i & -i return res def update_delta(self, i, delta): bit = self.bit l = len(bit) while i < l: bit[i] += delta i += i & -i
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well. Chefland is a big city which consists of infinite number of junctions on a 2D plane. The junctions are depicted below and they form a hexagonal grid — they satisfy the following conditions: Two of the junctions have coordinates $(0,0)$ and $(1,0)$. Each junction $J$ is connected by line segments with six other junctions. These junctions are the vertices of a regular hexagon with side length $1$ and their distances from $J$ are also all equal to $1$. Chef created a robot that can traverse the junctions. Initially, it is in some junction and oriented towards another junction which is connected to it by a line segment. The robot accepts a string of instructions — decimal digits $0$ through $5$. It executes the instructions one by one and stops after executing the final instruction. When the robot executes an instruction denoted by a digit $d$, it first rotates by $60 \cdot d$ degrees counterclockwise and then moves $1$ unit forward, to the junction it is oriented towards. Chef wants to test the robot, so he wrote a string $S$ of $N$ instructions. You should answer $Q$ queries. In each query, Chef wants to give the robot a substring of instructions $S_{L}, S_{L+1}, \ldots, S_{R}$; the robot starts in the junction $(0, 0)$ and it is oriented towards $(1, 0)$. You should find the final position (the $x$- and $y$-coordinates) of the robot. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $Q$. The second line contains a single string $S$ with length $N$. $Q$ lines follow. Each of these lines contains two space-separated integers $L$ and $R$ describing a query. ------ Output ------ For each query, print a single line containing two space-separated real numbers — the coordinates of the robot. Your answer will be considered correct if the absolute or relative error of each coordinate does not exceed $10^{-6}$. Warning: Outputting more than 8 digits after floating point may result in exceeding output size limit and receiving "Runtime error" verdict. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N, Q ≤ 2 \cdot 10^{5}$ $1 ≤ L ≤ R ≤ N$ $S$ contains only characters '0', '1', '2', '3', '4' and '5' the sum of $N$ over all test cases does not exceed $10^{6}$ the sum of $Q$ over all test cases does not exceed $10^{6}$ ------ Example Input ------ 1 3 3 012 1 1 1 2 2 2 ------ Example Output ------ 1.00000000 0.00000000 1.50000000 0.86602540 0.50000000 0.86602540
dx = [1, 0, -1, -1, 0, 1] dy = [0, 1, 1, 0, -1, -1] sr3p2 = 3**0.5 / 2 coss = [1, 0.5, -0.5, -1, -0.5, 0.5] sins = [0, sr3p2, sr3p2, 0, -sr3p2, -sr3p2] for _ in range(int(input())): N, Q = map(int, input().split()) instr = input() cum = [None] * N dr = 0 x, y = 0, 0 for i in range(N): dr = (dr + int(instr[i])) % 6 x, y = x + dx[dr], y + dy[dr] cum[i] = dr, x, y for _ in range(Q): L, R = map(int, input().split()) if L > 1: ldr, lx, ly = cum[L - 2] else: ldr, lx, ly = 0, 0, 0 _, rx, ry = cum[R - 1] cx, cy = rx - lx, ry - ly x = cx + cy * 0.5 y = cy * sr3p2 print( "{0:.7f} {1:.7f}".format( coss[ldr] * x + sins[ldr] * y, -sins[ldr] * x + coss[ldr] * y ) )
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
class Solution: def subarraySum(self, nums, k): l_nums = len(nums) c = 0 state = dict() state[0] = 1 sum = 0 for idx in range(0, l_nums): sum += nums[idx] if sum - k in state: c += state[sum - k] state[sum] = (state[sum] if sum in state else 0) + 1 return c
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER RETURN VAR
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
class Solution: def subarraySum(self, nums, k): dic = {} numSum = 0 dic[0] = 1 ans = 0 for i in range(len(nums)): numSum += nums[i] if numSum - k in dic: ans += dic[numSum - k] if numSum in dic: dic[numSum] += 1 else: dic[numSum] = 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
class Solution: def subarraySum(self, nums, k): sum_map = {(0): 1} count = 0 total_sum = 0 total = 0 for numb in nums: total += numb count += sum_map.get(total - k, 0) sum_map[total] = sum_map.get(total, 0) + 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
class Solution: def subarraySum(self, nums, k): count, cur, res = {(0): 1}, 0, 0 for v in nums: cur += v res += count.get(cur - k, 0) count[cur] = count.get(cur, 0) + 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
class Solution: def subarraySum(self, nums, k): count = collections.Counter() count[0] = 1 ans = su = 0 for x in nums: su += x ans += count[su - k] count[su] += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
class Solution: def subarraySum(self, nums, k): cum_sum_dict = {} cum_sum_dict[0] = 1 left_sum = 0 count = 0 for t in nums: left_sum = left_sum + t if left_sum - k in cum_sum_dict: count = count + cum_sum_dict[left_sum - k] if left_sum in cum_sum_dict: cum_sum_dict[left_sum] = cum_sum_dict[left_sum] + 1 else: cum_sum_dict[left_sum] = 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) ans = [] for i in range(m): temp = [] for j in range(n): temp.append( sum( [ mat[r][c] for r in range(max(0, i - k), min(i + k + 1, m)) for c in range(max(0, j - k), min(n, j + k + 1)) ] ) ) ans.append(temp) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: sum_mtx = [[(0) for col in range(len(mat[0]))] for row in range(len(mat))] for row in range(len(mat)): summ = 0 for col in range(len(mat[0])): summ += mat[row][col] sum_mtx[row][col] = summ if row > 0: sum_mtx[row][col] += sum_mtx[row - 1][col] output_img = [[(0) for col in range(len(mat[0]))] for row in range(len(mat))] for row in range(len(mat)): for col in range(len(mat[0])): max_row = min(len(mat) - 1, K + row) min_row = max(0, row - K) min_col = max(col - K, 0) max_col = min(col + K, len(mat[0]) - 1) a_val, b_val, c_val = 0, 0, 0 d_val = sum_mtx[max_row][max_col] if min_col > 0: c_val = sum_mtx[max_row][min_col - 1] if min_row > 0: b_val = sum_mtx[min_row - 1][max_col] if c_val and b_val: a_val = sum_mtx[min_row - 1][min_col - 1] output_img[row][col] = d_val - b_val - c_val + a_val return output_img
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: M = len(mat) N = len(mat[0]) def get_sum(i, j): def get_range(v, lim): return [x for x in range(v - K, v + K + 1) if x >= 0 and x < lim] ir = get_range(i, M) jr = get_range(j, N) return sum([mat[x][y] for x in ir for y in jr]) res = [] for i in range(len(mat)): res.append([]) for j in range(len(mat[i])): res[i].append(get_sum(i, j)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF FUNC_DEF RETURN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, A: List[List[int]], K: int) -> List[List[int]]: m, n = len(A), len(A[0]) def force_inside(ra, rb, ca, cb): return max(0, ra), min(m, rb), max(0, ca), min(n, cb) def rect(i, j): return force_inside(i - K, i + K + 1, j - K, j + K + 1) def sum_rect(ra, rb, ca, cb): ra, rb, ca, cb = force_inside(ra, rb, ca, cb) s = 0 for p in range(ra, rb): for q in range(ca, cb): s += A[p][q] return s S = [[(0) for j in range(n)] for i in range(m)] for i in range(m): for j in range(n): ra, rb, ca, cb = rect(i, j) if i == 0 and j == 0: S[i][j] = sum_rect(ra, rb, ca, cb) else: if j == 0: i0, j0 = i - 1, 0 else: i0, j0 = i, j - 1 ra0, rb0, ca0, cb0 = rect(i0, j0) S[i][j] = S[i0][j0] S[i][j] -= sum_rect(ra0, ra, ca, cb) S[i][j] -= sum_rect(ra, rb, ca0, ca) S[i][j] += sum_rect(rb0, rb, ca, cb) S[i][j] += sum_rect(ra, rb, cb0, cb) return S
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) ans = [([0] * n) for i in range(m)] def in_range(i, j): return i >= 0 and i < m and j >= 0 and j < n ans[0][0] = sum( [ mat[i][j] for i in range(0, K + 1) for j in range(0, K + 1) if in_range(i, j) ] ) for r in range(1, m): add = sum([mat[r + K][j] for j in range(0, K + 1) if r + K < m and j < n]) sub = sum( [mat[r - K - 1][j] for j in range(0, K + 1) if r - K - 1 >= 0 and j < n] ) ans[r][0] = ans[r - 1][0] + add - sub for r in range(0, m): for c in range(1, n): add = sum( [ mat[i][c + K] for i in range(r - K, r + K + 1) if i >= 0 and i < m and c + K < n ] ) sub = sum( [ mat[i][c - K - 1] for i in range(r - K, r + K + 1) if i >= 0 and i < m and c - K - 1 >= 0 ] ) ans[r][c] = ans[r][c - 1] + add - sub return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) ans = [[(0) for i in range(n)] for j in range(m)] for i in range(m): for j in range(n): for l in range(max(0, i - K), min(i + K + 1, m)): ans[i][j] += sum(mat[l][max(0, j - K) : min(j + K + 1, n)]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) k = K row = [[(0) for _ in range(n)] for _ in range(m)] ans = [[(0) for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): for r in range(max(0, i - k), min(m, i + k + 1)): row[i][j] += mat[r][j] for i in range(m): for j in range(n): for c in range(max(0, j - k), min(n, j + k + 1)): ans[i][j] += row[i][c] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: cum = [[(0) for i in range(len(mat[0]))] for i in range(len(mat))] for i in range(len(mat)): for j in range(len(mat[i])): if j == 0: cum[i][j] = mat[i][j] else: cum[i][j] = cum[i][j - 1] + mat[i][j] ans = [[(0) for i in range(len(mat[0]))] for i in range(len(mat))] for i in range(len(mat)): for j in range(len(mat[0])): summ = 0 for k in range(max(0, i - K), min(len(mat), i + K + 1)): if j - K - 1 < 0: summ += cum[k][min(j + K, len(cum[i]) - 1)] else: summ += cum[k][min(j + K, len(cum[k]) - 1)] summ -= cum[k][j - K - 1] ans[i][j] = summ print(ans) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: n = len(mat) m = len(mat[0]) result = [[(0) for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m): if i == 0 and j == 0: for r in range(max(i - K, 0), min(i + K + 1, n)): for c in range(max(j - K, 0), min(j + K + 1, m)): result[i][j] += mat[r][c] elif j > 0: prev_c = j - 1 result[i][j] += result[i][prev_c] for r in range(max(i - K, 0), min(i + K + 1, n)): if j - K - 1 >= 0: result[i][j] -= mat[r][j - K - 1] if j + K < m: result[i][j] += mat[r][j + K] else: prev_r = i - 1 result[i][j] += result[prev_r][j] for c in range(max(j - K, 0), min(j + K + 1, m)): if i - K - 1 >= 0: result[i][j] -= mat[i - K - 1][c] if i + K < n: result[i][j] += mat[i + K][c] return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) result_mat = [] for i in range(m): result_mat.append([0] * n) for i in range(m): for j in range(n): result_mat[i][j] = sum( [ sum(mat[k][max(0, j - K) : min(n, j + K + 1)]) for k in range(max(0, i - K), min(m, i + K + 1)) ] ) return result_mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: blockSum = [([0] * len(mat[0])) for _ in range(len(mat))] accumSum = [([0] * (len(mat[0]) + 1)) for _ in range(len(mat) + 1)] for x in range(len(mat)): for y in range(len(mat[0])): accumSum[x + 1][y + 1] = ( (accumSum[x][y + 1] if x >= 0 else 0) + (accumSum[x + 1][y] if y >= 0 else 0) - (accumSum[x][y] if x >= 0 and y >= 0 else 0) + mat[x][y] ) for x in range(len(mat)): for y in range(len(mat[0])): blockSum[x][y] = ( accumSum[max(x - K, 0)][max(y - K, 0)] + accumSum[min(x + K + 1, len(mat))][min(y + K + 1, len(mat[0]))] - accumSum[max(x - K, 0)][min(y + K + 1, len(mat[0]))] - accumSum[min(x + K + 1, len(mat))][max(y - K, 0)] ) return blockSum
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: valid_row_indexes = range(len(mat)) valid_col_indexes = range(len(mat[0])) answer = [([0] * len(mat[0])) for _ in range(len(mat))] for r in valid_row_indexes: for c in valid_col_indexes: left = c - K if c - K in valid_col_indexes else None right = c + K + 1 if c + K in valid_col_indexes else None answer[r][c] += sum( [ sum(mat[i][left:right]) for i in range(r - K, r + K + 1) if i in valid_row_indexes ] ) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NONE VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: res_mat = [] M = len(mat) N = len(mat[0]) for i, m in enumerate(mat): res_list = [] for j, n in enumerate(m): val = 0 r_min = i - K if i - K > 0 else 0 r_max = i + K + 1 if i + K + 1 < M else M c_min = j - K if j - K > 0 else 0 c_max = j + K + 1 if j + K + 1 < N else N for k in range(r_min, r_max): for l in range(c_min, c_max): val += mat[k][l] res_list.append(val) res_mat.append(res_list) return res_mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: cumMat = [[(0) for _ in range(len(mat[0]) + 2)] for _ in range(len(mat) + 2)] cumMat[1][1] = mat[0][0] for i in range(2, len(mat) + 1): cumMat[i][1] = mat[i - 1][0] + cumMat[i - 1][1] for j in range(2, len(mat[0]) + 1): cumMat[1][j] = mat[0][j - 1] + cumMat[1][j - 1] for i in range(2, len(mat) + 1): for j in range(2, len(mat[0]) + 1): cumMat[i][j] = ( mat[i - 1][j - 1] + cumMat[i - 1][j] + cumMat[i][j - 1] - cumMat[i - 1][j - 1] ) answer = [[(0) for _ in mat[0]] for _ in mat] for i in range(1, len(cumMat) - 1): for j in range(1, len(cumMat[0]) - 1): i1 = max(1, i - K) i2 = min(len(cumMat) - 2, i + K) j1 = max(1, j - K) j2 = min(len(cumMat[0]) - 2, j + K) answer[i - 1][j - 1] = ( cumMat[i2][j2] - cumMat[i1 - 1][j2] - cumMat[i2][j1 - 1] + cumMat[i1 - 1][j1 - 1] ) print(answer[i - 1][j - 1]) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: M, N = len(mat), len(mat[0]) tsum, lsum = [[(0) for _ in range(N)] for _ in range(M)], [ [(0) for _ in range(N)] for _ in range(M) ] for i in range(M): for j in range(N): tsum[i][j] = (0 if i == 0 else tsum[i - 1][j]) + mat[i][j] lsum[i][j] = (0 if j == 0 else lsum[i][j - 1]) + mat[i][j] ans = [[(0) for _ in range(N)] for _ in range(M)] def l(i, j): if j >= N: return l(i, N - 1) elif 0 <= i < M and 0 <= j < N: return lsum[i][j] return 0 def t(i, j): if i >= M: return t(M - 1, j) elif 0 <= i < M and 0 <= j < N: return tsum[i][j] return 0 for i in range(min(M, K + 1)): for j in range(min(N, K + 1)): ans[0][0] += mat[i][j] for i in range(1, M): ans[i][0] = ans[i - 1][0] + l(i + K, K) - l(i - K - 1, K) for j in range(1, N): ans[0][j] = ans[0][j - 1] + t(K, j + K) - t(K, j - K - 1) for i in range(1, M): for j in range(1, N): tl = ans[i - 1][j - 1] add = ( l(i + K, j + K) - l(i + K, j - K - 1) + t(i + K - 1, j + K) - t(i - K - 1, j + K) ) sub = ( t(i + K - 1, j - K - 1) - t(i - K - 2, j - K - 1) + l(i - K - 1, j + K - 1) - l(i - K - 1, j - K - 1) ) ans[i][j] = tl + add - sub return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF NUMBER VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: r, c = len(mat), len(mat[0]) rsums = [[0] for i in range(r)] for i in range(r): for j in range(c): rsums[i].append(rsums[i][-1] + mat[i][j]) for i in range(r): for j in range(c): mat[i][j] = 0 lr = 0 if i - k < 0 else i - k rr = r - 1 if i + k >= r else i + k lc = 0 if j - k < 0 else j - k rc = c - 1 if j + k >= c else j + k rc += 1 for x in range(lr, rr + 1): mat[i][j] += rsums[x][rc] - rsums[x][lc] return mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: res = [] t = [] for i in range(len(mat)): t.append([0] * len(mat[0])) res.append([0] * len(mat[0])) for j in range(len(mat[0])): t[i][j] = mat[i][j] if j > 0: mat[i][j] += mat[i][j - 1] for i in range(len(mat)): for j in range(len(mat[0])): s = 0 top = i - K bot = i + K left = j - K if left < 0: left = 0 right = j + K if right >= len(mat[0]): right = len(mat[0]) - 1 for k in range(top, bot + 1): if k >= 0 and k < len(mat): s += mat[k][right] - mat[k][left] + t[k][left] res[i][j] = s return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: if not mat: return m = len(mat) n = len(mat[0]) sumMat = [[(0) for i in range(n)] for j in range(m)] verMat = [[(0) for i in range(n)] for j in range(m)] ansMat = [[(0) for i in range(n)] for j in range(m)] fnlMat = [[(0) for i in range(n)] for j in range(m)] for i in range(m): running_sum = 0 for j in range(n): running_sum += mat[i][j] sumMat[i][j] = running_sum for i in range(m): for j in range(n): verMat[i][j] = self.sumRowWindow(sumMat, i, j, K) for j in range(n): running_sum = 0 for i in range(m): running_sum += verMat[i][j] ansMat[i][j] = running_sum for i in range(m): for j in range(n): fnlMat[i][j] = self.sumColWindow(ansMat, i, j, K) return fnlMat def sumColWindow(self, verMat, row_i, col_j, window_k): col = [verMat[i][col_j] for i in range(len(verMat))] window_left = max(-1, row_i - window_k - 1) window_right = min(len(col) - 1, row_i + window_k) if window_left == -1: return col[window_right] return col[window_right] - col[window_left] def sumRowWindow(self, sumMat, row_i, col_j, window_k): row = sumMat[row_i] window_left = max(-1, col_j - window_k - 1) window_right = min(len(row) - 1, col_j + window_k) if window_left == -1: return row[window_right] return row[window_right] - row[window_left]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: rows, cols = len(mat), len(mat[0]) dic = {} for i, row in enumerate(mat): prefix_sum = [0] * (cols + 1) for j, num in enumerate(row): prefix_sum[j] = prefix_sum[j - 1] + num dic[i] = prefix_sum for i in range(rows): for j in range(cols): start_col, end_col = max(j - K, 0), min(j + K, cols - 1) start_row, end_row = max(i - K, 0), min(i + K, rows - 1) res = 0 for idx in range(start_row, end_row + 1): res += dic[idx][end_col] - dic[idx][start_col - 1] mat[i][j] = res return mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) answer = [[(0) for i in range(n)] for i in range(m)] for i in range(n): for j in range(m): c_lower = max(0, i - K) c_upper = min(i + K + 1, n) r_lower = max(0, j - K) r_upper = min(j + K + 1, m) print((m, n)) print(((r_lower, r_upper), (c_lower, c_upper))) answer[j][i] = sum( [ mat[r][c] for r in range(r_lower, r_upper) for c in range(c_lower, c_upper) ] ) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) ans = [[(0) for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(1, n): mat[i][j] += mat[i][j - 1] for i in range(m): for j in range(n): res = 0 for block_row in range(max(0, i - K), min(m, i + K + 1)): res += mat[block_row][min(n - 1, j + K)] if j - K - 1 >= 0: res -= mat[block_row][j - K - 1] ans[i][j] = res return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: if not mat: return [] m = len(mat) n = len(mat[0]) get = lambda i, j: mat[i][j] if 0 <= i < m and 0 <= j < n else 0 row = lambda i, j: sum([get(i, y) for y in range(j - K, j + K + 1)]) col = lambda i, j: sum([get(x, j) for x in range(i - K, i + K + 1)]) answer = [([0] * n) for _ in range(m)] for i in range(0, K + 1): for j in range(0, K + 1): answer[0][0] += get(i, j) for i in range(m): for j in range(n): if i == 0 and j == 0: continue if j == 0: answer[i][j] = answer[i - 1][j] - row(i - K - 1, j) + row(i + K, j) else: answer[i][j] = answer[i][j - 1] - col(i, j - K - 1) + col(i, j + K) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: M = len(mat) if M == 0: return mat N = len(mat[0]) for i in range(1, M): mat[i][0] += mat[i - 1][0] for j in range(1, N): mat[0][j] += mat[0][j - 1] for i in range(1, M): for j in range(1, N): mat[i][j] = ( mat[i][j] + mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1] ) answer = [[(0) for _ in range(N)] for _ in range(M)] for i in range(M): for j in range(N): rl = max(i - K, 0) cl = max(j - K, 0) rr = min(i + K, M - 1) cr = min(j + K, N - 1) A, B, C, D = 0, 0, 0, mat[rr][cr] if rl - 1 >= 0: B = mat[rl - 1][cr] if cl - 1 >= 0: C = mat[rr][cl - 1] if rl - 1 >= 0 and cl - 1 >= 0: A = mat[rl - 1][cl - 1] answer[i][j] = D + A - B - C return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: rc, cc = len(mat), len(mat[0]) acc = [([0] * (cc + 1)) for _ in range(rc + 1)] for r in range(rc): for c in range(cc): acc[r + 1][c + 1] = ( acc[r][c + 1] + acc[r + 1][c] - acc[r][c] + mat[r][c] ) res = [([0] * cc) for _ in range(rc)] for r in range(rc): for c in range(cc): res[r][c] = ( acc[min(r + K + 1, rc)][min(c + K + 1, cc)] - acc[max(r - K, 0)][min(c + 1 + K, cc)] - acc[min(r + 1 + K, rc)][max(c - K, 0)] + acc[max(r - K, 0)][max(c - K, 0)] ) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) arr = [[(0) for i in range(n + 1)] for i in range(m)] for i in range(m): for j in range(n): arr[i][j + 1] = arr[i][j] + mat[i][j] narr = [[(0) for i in range(n)] for i in range(m)] for i in range(m): for j in range(n): x1, x2, y1, y2 = ( max(0, j - k), min(n - 1, j + k), max(0, i - k), min(m - 1, i + k), ) a = 0 for t in range(y1, y2 + 1): a += arr[t][x2 + 1] - arr[t][x1] narr[i][j] = a return narr
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) mat[:] = [[0] * (n + 1)] + [([0] + row) for row in mat] res = [[(0) for _ in range(n)] for _ in range(m)] for i in range(1, m + 1): for j in range(1, n + 1): mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1] for i in range(m): for j in range(n): r1, c1 = max(i - K, 0), max(j - K, 0) r2, c2 = min(i + K + 1, m), min(j + K + 1, n) res[i][j] = mat[r2][c2] - mat[r1][c2] - mat[r2][c1] + mat[r1][c1] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) arr = [] for i in range(m): temp = [] for j in range(n): s = 0 r_s = i - k r_e = i + k + 1 if r_s < 0: r_s = 0 if r_e > m: r_e = m c_s = j - k c_e = j + k + 1 if c_s < 0: c_s = 0 if c_e > n: c_e = n for ki in range(r_s, r_e): for l in range(c_s, c_e): s = mat[ki][l] + s temp.append(s) arr.append(temp) return arr
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: height = len(mat) width = len(mat[0]) z = [[None for _ in range(width)] for _ in range(height)] for x in range(width): for y in range(height): z[y][x] = sum( [ i for j in [ row[max(x - K, 0) : x + K + 1] for row in mat[max(y - K, 0) : y + K + 1] ] for i in j ] ) return z
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: arr = [[(0) for _ in range(len(mat[0]))] for _ in range(len(mat))] def addsum(i, j): i_start = i - k i_end = i + k j_start = j - k j_end = j + k ans = 0 if i - k < 0: i_start = 0 if i + k >= len(mat): i_end = len(mat) - 1 if j - k < 0: j_start = 0 if j + k >= len(mat[0]): j_end = len(mat[0]) - 1 for m in range(i_start, i_end + 1): for n in range(j_start, j_end + 1): ans += mat[m][n] arr[i][j] = ans return for i in range(len(mat)): for j in range(len(mat[0])): addsum(i, j) return arr
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
from itertools import accumulate class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: A = mat.copy() for i in range(len(A)): A[i] = [0] + list(accumulate(A[i])) Ans = [[(0) for i in range(len(A[0]))] for j in range(len(A))] for i in range(len(A)): for j in range(1, len(A[0])): for p in range(max(0, i - K), min(len(A) - 1, i + K) + 1): Ans[i][j] += ( A[p][min(j + K, len(A[0]) - 1)] - A[p][max(0, j - K - 1)] ) for i in range(len(Ans)): Ans[i] = Ans[i][1:] return Ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) for r in range(m): mat[r] = [0] * K + mat[r] + [0] * K for i in range(K): mat = [[0] * (n + 2 * K)] + mat + [[0] * (n + 2 * K)] ret = [] print(mat) for i in range(m): this = [] for j in range(n): I, J = i + K, j + K add = sum( [sum(mat[r][J - K : J + K + 1]) for r in range(I - K, I + K + 1)] ) this.append(add) ret.append(this) return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP LIST NUMBER VAR VAR VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP LIST NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR LIST BIN_OP LIST NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: if not mat: self.cum_matrix = [[]] else: nrow = len(mat) ncol = len(mat[0]) self.cum_matrix = [([0] * ncol) for _ in range(nrow)] self.cum_matrix[0][0] = mat[0][0] for i in range(1, ncol): self.cum_matrix[0][i] = self.cum_matrix[0][i - 1] + mat[0][i] for j in range(1, nrow): self.cum_matrix[j][0] = self.cum_matrix[j - 1][0] + mat[j][0] for c in range(1, ncol): for r in range(1, nrow): self.cum_matrix[r][c] = ( self.cum_matrix[r - 1][c] + self.cum_matrix[r][c - 1] - self.cum_matrix[r - 1][c - 1] + mat[r][c] ) res = [([0] * ncol) for _ in range(nrow)] if not self.cum_matrix: return [] for r in range(nrow): for c in range(ncol): row1 = max(0, r - K) col1 = max(0, c - K) row2 = min(nrow - 1, r + K) col2 = min(ncol - 1, c + K) if row1 == 0 and col1 == 0: res[r][c] = self.cum_matrix[row2][col2] elif row1 == 0 and col1 > 0: res[r][c] = ( self.cum_matrix[row2][col2] - self.cum_matrix[row2][col1 - 1] ) elif row1 > 0 and col1 == 0: res[r][c] = ( self.cum_matrix[row2][col2] - self.cum_matrix[row1 - 1][col2] ) else: res[r][c] = ( self.cum_matrix[row2][col2] - self.cum_matrix[row1 - 1][col2] - self.cum_matrix[row2][col1 - 1] + self.cum_matrix[row1 - 1][col1 - 1] ) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR ASSIGN VAR LIST LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR IF VAR RETURN LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: N, M = len(mat), len(mat[0]) ans = [([0] * M) for i in range(N)] def getBlockSum(i, j): rows = mat[max(0, i - K) : i + K + 1] block = list(zip(*rows))[max(0, j - K) : j + K + 1] return sum(sum(blockrow) for blockrow in block) for i in range(N): for j in range(M): ans[i][j] = getBlockSum(i, j) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: dp = [] result = [] for y in range(len(mat)): dp.append([]) s = 0 for x in range(len(mat[y])): s += mat[y][x] dp[y].append(s) for y in range(len(mat)): result.append([]) for x in range(len(mat[y])): cur = 0 for y1 in range(max(y - K, 0), min(y + K + 1, len(mat))): if x - K - 1 >= 0: cur -= dp[y1][x - K - 1] cur += ( dp[y1][len(mat[y1]) - 1] if x + K >= len(mat[y1]) else dp[y1][x + K] ) result[y].append(cur) return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) answer = [[(0) for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): lower_row = max(0, i - K) upper_row = min(m - 1, i + K) left_col = max(0, j - K) right_col = min(n - 1, j + K) total = 0 for x in range(lower_row, upper_row + 1): for y in range(left_col, right_col + 1): total += mat[x][y] answer[i][j] = total return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: iMax = len(mat) jMax = len(mat[0]) ans = [[(0) for j in range(jMax)] for i in range(iMax)] i = 0 j = 0 ans[i][j] = sum( [ sum([mat[i0][j0] for j0 in range(j, min(jMax, j + K + 1))]) for i0 in range(i, min(iMax, i + K + 1)) ] ) i = 0 for j in range(1, jMax): ans[i][j] = ans[i][j - 1] if j + K < jMax: ans[i][j] += sum( [ mat[i0][j + K] for i0 in range(max(0, i - K), min(iMax, i + K + 1)) ] ) if j - K - 1 >= 0: ans[i][j] -= sum( [ mat[i0][j - K - 1] for i0 in range(max(0, i - K), min(iMax, i + K + 1)) ] ) j = 0 for i in range(1, iMax): ans[i][j] = ans[i - 1][j] if i + K < iMax: ans[i][j] += sum( [ mat[i + K][j0] for j0 in range(max(0, j - K), min(jMax, j + K + 1)) ] ) if i - K - 1 >= 0: ans[i][j] -= sum( [ mat[i - K - 1][j0] for j0 in range(max(0, j - K), min(jMax, j + K + 1)) ] ) for i in range(1, iMax): for j in range(1, jMax): ans[i][j] = ans[i][j - 1] if j + K < jMax: ans[i][j] += sum( [ mat[i0][j + K] for i0 in range(max(0, i - K), min(iMax, i + K + 1)) ] ) if j - K - 1 >= 0: ans[i][j] -= sum( [ mat[i0][j - K - 1] for i0 in range(max(0, i - K), min(iMax, i + K + 1)) ] ) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: answer = [[None for _ in range(len(mat[0]))] for __ in range(len(mat))] prefix_sum = [[(0) for _ in range(len(mat[0]))] for __ in range(len(mat))] for i in range(len(mat)): for j in range(len(mat[0])): prefix_sum[i][j] += mat[i][j] prefix_sum[i][j] += prefix_sum[i - 1][j] if 0 <= i - 1 else 0 prefix_sum[i][j] += prefix_sum[i][j - 1] if 0 <= j - 1 else 0 prefix_sum[i][j] -= ( prefix_sum[i - 1][j - 1] if 0 <= i - 1 and 0 <= j - 1 else 0 ) print(prefix_sum) for i in range(len(mat)): for j in range(len(mat[0])): bottom_right = prefix_sum[min(i + K, len(mat) - 1)][ min(j + K, len(mat[0]) - 1) ] bottom_left = ( prefix_sum[min(i + K, len(mat) - 1)][j - K - 1] if j - K - 1 >= 0 else 0 ) top_left = ( prefix_sum[i - K - 1][j - K - 1] if 0 <= i - K - 1 and 0 <= j - K - 1 else 0 ) top_right = ( prefix_sum[i - K - 1][min(j + K, len(mat[0]) - 1)] if 0 <= i - K - 1 else 0 ) answer[i][j] = bottom_right - bottom_left - top_right + top_left return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: row_size, col_size = len(mat), len(mat[0]) ans = [[(0) for _ in range(len(mat[0]))] for _ in range(len(mat))] for x in mat: for _ in range(K): x.append(0) x.insert(0, 0) row = [[(0) for _ in range(len(mat[0]))] for _ in range(K)] mat = row + mat + row print(mat) for r in range(K, K + row_size): for c in range(K, K + col_size): ans[r - K][c - K] = sum( sum(s[c - K : c + K + 1]) for s in mat[r - K : r + K + 1] ) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) padded_mat = [] for i in range(K): padded_mat.append([0] * (n + 2 * K)) for i in range(m): padded_mat.append([0] * K + mat[i] + [0] * K) for i in range(K): padded_mat.append([0] * (n + 2 * K)) sum_mat = [] for i in range(m): sum_mat.append([0] * n) for i in range(m): for j in range(n): sum_mat[i][j] = sum( [ sum(row[j : j + 2 * K + 1]) for row in padded_mat[i : i + 2 * K + 1] ] ) return sum_mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP LIST NUMBER VAR VAR VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) ans = [([0] * n) for i in range(m)] prefix = [([0] * (n + 1)) for i in range(m)] for i in range(m): for j in range(n): prefix[i][j] = prefix[i][j - 1] + mat[i][j] for i in range(m): for j in range(n): for s in range(k + 1): if i - s < 0: break ans[i][j] += ( prefix[i - s][min(n - 1, j + k)] - prefix[i - s][max(-1, j - k - 1)] ) for s in range(1, k + 1): if i + s > m - 1: break ans[i][j] += ( prefix[i + s][min(n - 1, j + k)] - prefix[i + s][max(-1, j - k - 1)] ) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) if K >= max(m - 1, n - 1): return [([sum([sum(i) for i in mat])] * n) for _ in range(m)] res = [([0] * n) for _ in range(m)] for r in range(m): for c in range(n): res[r][c] = sum( [ sum(i[max(c - K, 0) : min(n, c + K + 1)]) for i in mat[max(0, r - K) : min(m, r + K + 1)] ] ) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP LIST FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: answer = [] r = 0 c = 0 for i in range(len(mat)): tmp = [] for j in range(len(mat[0])): if i - K < 0: r = 0 else: r = i - K sum1 = 0 while r <= i + K and r < len(mat): if j - K < 0: if j + K < len(mat[0]): sum1 += sum(mat[r][: j + K + 1]) else: sum1 += sum(mat[r]) elif j + K < len(mat[0]): sum1 += sum(mat[r][j - K : j + K + 1]) else: sum1 += sum(mat[r][j - K :]) r += 1 tmp.append(sum1) answer.append(tmp) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: rows = collections.defaultdict(list) for i, row in enumerate(mat): s = 0 for v in row: s += v rows[i].append(s) m, n = len(mat), len(mat[0]) ans = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): ind = [max(0, j - K), min(n - 1, j + K)] for k in range(max(0, i - K), min(m, i + K + 1)): sum = rows[k][ind[1]] if ind[0] > 0: sum = rows[k][ind[1]] - rows[k][ind[0] - 1] ans[i][j] += sum return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: rows, cols = len(mat), len(mat[0]) dp = [([0] * cols) for _ in range(rows)] def g(x, y): nonlocal rows, cols if x < 0 or y < 0: return 0 return dp[min(x, rows - 1)][min(y, cols - 1)] for i in range(rows): for j in range(cols): dp[i][j] = mat[i][j] + g(i - 1, j) + g(i, j - 1) - g(i - 1, j - 1) ans = [([0] * cols) for _ in range(rows)] for i in range(rows): for j in range(cols): ans[i][j] = ( g(i + K, j + K) + g(i - K - 1, j - K - 1) - g(i + K, j - K - 1) - g(i - K - 1, j + K) ) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: if not mat or not mat[0]: return mat R, C = len(mat), len(mat[0]) l2r = [([0] * C) for _ in range(R)] t2b = [([0] * C) for _ in range(R)] for r in range(R): for c in range(C): l2r[r][c] += mat[r][c] if c != 0: l2r[r][c] += l2r[r][c - 1] t2b[r][c] += mat[r][c] if r != 0: t2b[r][c] += t2b[r - 1][c] ret = [([0] * C) for _ in range(R)] for r in range(R): for c in range(C): temp = 0 left = max(0, c - K) right = min(C - 1, c + K) for _r in range(max(0, r - K), min(R, r + K + 1)): if left == 0: temp += l2r[_r][right] else: temp += l2r[_r][right] - l2r[_r][left - 1] ret[r][c] = temp return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: mat2 = copy.deepcopy(mat) def help(i, j, K): res = 0 for ii in range(i - K, i + K + 1): for jj in range(j - K, j + K + 1): if 0 <= ii < len(mat) and 0 <= jj < len(mat[0]): res += mat[ii][jj] return res for i in range(len(mat)): for j in range(len(mat[0])): if j == 0: mat2[i][j] = help(i, j, K) else: mat2[i][j] = mat2[i][j - 1] if j + K < len(mat[0]): for ii in range(i - K, i + K + 1): if 0 <= ii < len(mat): mat2[i][j] += mat[ii][j + K] if j - K - 1 >= 0: for ii in range(i - K, i + K + 1): if 0 <= ii < len(mat): mat2[i][j] -= mat[ii][j - K - 1] return mat2
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) answer = [([0] * n) for _ in range(m)] dp = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): if j == 0: dp[i][j] = mat[i][j] else: dp[i][j] = dp[i][j - 1] + mat[i][j] for i in range(m): for j in range(n): for x in range(i - K, i + K + 1): if x >= 0 and x < m: if j - K <= 0: if j + K >= n: answer[i][j] += dp[x][-1] else: answer[i][j] += dp[x][j + K] elif j - K <= n: if j + K >= n: answer[i][j] += dp[x][-1] - dp[x][j - K - 1] else: answer[i][j] += dp[x][j + K] - dp[x][j - K - 1] return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) answer = [] temp = [] val, i, j = 0, 0, 0 for r in range(max(0, i - K), min(i + K + 1, m)): for c in range(max(0, j - K), min(j + K + 1, n)): val += mat[r][c] temp.append(val) for i in range(m): if i == 0: row = temp else: row = [] r1 = i - K - 1 r2 = i + K subs2, adds2 = 0, 0 for c in range(max(0, -K), min(K + 1, n)): if r1 >= 0: subs2 += mat[r1][c] if r2 < m: adds2 += mat[r2][c] row.append(answer[-1][0] + adds2 - subs2) for j in range(1, n): subs, adds = 0, 0 c1 = j - K - 1 c2 = j + K for r in range(max(0, i - K), min(i + K + 1, m)): if c1 >= 0: subs += mat[r][c1] if c2 < n: adds += mat[r][c2] row.append(row[-1] + adds - subs) answer.append(row) return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: def matsum(mat, x1, y1, x2, y2): sumi = 0 for row in mat[x1 : x2 + 1]: sumi += sum(row[y1 : y2 + 1]) return sumi newmat = [([0] * len(row)) for row in mat] for ri, row in enumerate(mat): for ci, i in enumerate(row): newmat[ri][ci] = matsum( mat, max(0, ri - K), max(0, ci - K), min(len(mat) - 1, ri + K), min(len(row) - 1, ci + K), ) return newmat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR VAR
Given a m * n matrix mat and an integer K, return a matrix answer where each answer[i][j] is the sum of all elements mat[r][c] for i - K <= r <= i + K, j - K <= c <= j + K, and (r, c) is a valid position in the matrix.   Example 1: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 1 Output: [[12,21,16],[27,45,33],[24,39,28]] Example 2: Input: mat = [[1,2,3],[4,5,6],[7,8,9]], K = 2 Output: [[45,45,45],[45,45,45],[45,45,45]]   Constraints: m == mat.length n == mat[i].length 1 <= m, n, K <= 100 1 <= mat[i][j] <= 100
class Solution: def getArrIJ(self, i: int, j: int, arr: List[List[int]]) -> int: if i >= len(arr) and j >= len(arr[0]): return arr[len(arr) - 1][len(arr[0]) - 1] if i >= len(arr): return arr[len(arr) - 1][j] if j >= len(arr[0]): return arr[i][len(arr[0]) - 1] if i < 0 and j < 0: return arr[0][0] if i < 0: return arr[0][j] if j < 0: return arr[i][0] return arr[i][j] def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) res = [] for i in range(K + 1): res.append([0] * (n + 2 * (K + 1))) for i in range(m): res.append([0] * (K + 1) + mat[i] + [0] * (K + 1)) for i in range(K + 1): res.append([0] * (n + 2 * (K + 1))) for i in range(1, m + 2 * (K + 1)): for j in range(1, n + 2 * (K + 1)): res[i][j] = ( res[i][j] + res[i - 1][j] + res[i][j - 1] - res[i - 1][j - 1] ) for i in range(K + 1, m + K + 1): for j in range(K + 1, n + K + 1): mat[i - K - 1][j - K - 1] = res[i + K][j + K] - ( res[i + K][j - K - 1] + res[i - K - 1][j + K] - res[i - K - 1][j - K - 1] ) return mat
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR NUMBER RETURN VAR NUMBER VAR IF VAR NUMBER RETURN VAR VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR VAR