description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): def answer(root): if root == None: return float("-inf") if root.left == None and root.right == None: return root.data 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 FUNC_CALL VAR STRING IF VAR NONE VAR NONE RETURN VAR 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
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): def traverse(temp, l, s): if temp == None: return if temp.left is None and temp.right is None: s += temp.data l.append(s) return s += temp.data traverse(temp.left, l, s) traverse(temp.right, l, s) l = [] temp = root s = 0 traverse(temp, l, s) return max(l)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
import sys class Solution: def maxPathSum(self, root): import sys def cal(root): if root == None: return -sys.maxsize if root.left == root.right == None: return root.data ls = cal(root.left) rs = cal(root.right) return max(ls, rs) + root.data return cal(root)
IMPORT CLASS_DEF FUNC_DEF IMPORT FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): if root is None: return l = self.maxPathSum(root.left) r = self.maxPathSum(root.right) if l is None and r is None: return root.data if l is None: return r + root.data if r is None: return l + root.data return max(l, r) + root.data
CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE RETURN BIN_OP VAR VAR IF VAR NONE RETURN BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): if not root: return 0 def cursor(r, s): if not r: return None s += r.data if r.left == None and r.right == None: return s left = cursor(r.left, s) right = cursor(r.right, s) if left != None and right != None: return max(left, right) return left if left else right return cursor(root, 0)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER FUNC_DEF IF VAR RETURN NONE VAR VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR NONE RETURN FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): if not root: return -1 sum = root.data myList = [] def helper(root, sum): if not root: return 0 if not root.left and not root.right: if len(myList) == 0: myList.append(sum) elif myList[0] < sum: myList[0] = sum if root.left: helper(root.left, sum + root.left.data) if root.right: helper(root.right, sum + root.right.data) return myList return helper(root, sum)[0]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): def trav(summ, root): if not root: return 0 summ += root.data if not root.left and not root.right: self.maxx = max(self.maxx, summ) trav(summ, root.left) trav(summ, root.right) summ -= root.data self.maxx = float("-inf") trav(0, root) return self.maxx
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): def solve(root, dic, lis): if root == None: return None lis.append(root.data) if root.left == None and root.right == None: dic.append(sum(lis)) solve(root.left, dic, lis) solve(root.right, dic, lis) lis.pop() return dic lis = solve(root, [], []) res = -100000000 return max(lis) for i in lis: res = max(res, sum(i)) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR IF VAR NONE VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR LIST LIST ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
INT_MIN = -(1 << 63) class Solution: def maxPathSum(self, root): if root is None: return INT_MIN if root.left is None and root.right is None: return root.data ls = self.maxPathSum(root.left) rs = self.maxPathSum(root.right) return root.data + max(ls, rs)
ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR
Given a Binary Tree, find the maximum sum path from a leaf to root. Example 1: Input: 1 / \ 2 3 Output: 4 Explanation: Following the path 3 -> 1, results in a sum of 4, which is the maximum path sum from leaf to root for the given tree. Example 2: Input: 10 / \ -2 7 / \ 8 -4 Output: 17 Explanation : Following the path 7 -> 10, results in a sum of 17, which is the maximum path sum from leaf to root for the given tree. Your task : You don't need to read input or print anything. Your task is to complete the function maxPathSum() which takes the root node of the tree as input and returns an integer denoting the maximum possible leaf to root path sum. Expected Time Complexity: O(n) , where n = number of nodes Expected Auxiliary Space: O(1) Constraints : 1 <= Number of nodes <= 10^5 -10^{6} <= max sum path <= 10^{6}
class Solution: def maxPathSum(self, root): if not root: return 0 else: p = root.data if root.left and root.right: return p + max(self.maxPathSum(root.left), self.maxPathSum(root.right)) elif root.left: return p + self.maxPathSum(root.left) elif root.right: return p + self.maxPathSum(root.right) else: return p
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR IF VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def f(l, n, c, k): x = 0 if k == n: return c m = -1 for i in range(k, n): if m < l[i]: m = l[i] x = i c += 1 a, b = f(l, n, c, x + 1), f(l, x, c, k) if a < b: return a else: return b def f1(l: list, s: int, e: int): m = -1 k = -1 for i in range(s, e + 1): if l[i] > m: m = l[i] k = i if m in (l[s], l[e]): return 1 else: return 1 + min(f1(l, s, k - 1), f1(l, k + 1, e)) t = int(input()) for i in range(t): n = int(input()) a = [int(x) for x in input().split()] c, k = 0, 0 print(f1(a, 0, n - 1))
FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def select_hill(hills): m = hills.index(max(hills)) if m == 0 or m == len(hills) - 1: return 1 else: return 1 + min(select_hill(hills[:m]), select_hill(hills[m + 1 :])) t = int(input()) for _ in range(t): n = int(input()) hills = list(map(int, input().split())) print(select_hill(hills))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) def func(l): m = max(l) m = l.index(m) if m == len(l) - 1 or m == 0: return 1 ll = l[:m] rl = l[m + 1 :] return 1 + min(func(rl), func(ll)) print(func(l))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def res(h): i = 0 m = 0 for ind, val in enumerate(h): if val > m: m = val i = ind l = len(h) if i == 0 or i == l - 1: return 1 return 1 + min([res(h[i + 1 :]), res(h[:i])]) for _ in range(int(input())): n = int(input()) h = list(map(int, input().split())) print(res(h))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def min_pumps(lst): if not lst: return 0 r = 0 for i in range(len(lst)): if lst[i] > lst[r]: r = i if r == 0 or r == len(lst): return 1 return 1 + min(min_pumps(lst[:r]), min_pumps(lst[r + 1 :])) for _ in range(int(input())): input() hills = list(map(int, input().split())) print(min_pumps(hills))
FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
t = int(input()) def pump(arr, n): count = 0 maxHeight = arr.index(max(arr)) if maxHeight == 0 or maxHeight == n - 1: return 1 leftRes = pump(arr[:maxHeight], maxHeight) rightRes = pump(arr[maxHeight + 1 :], n - maxHeight - 1) if leftRes > rightRes: count = count + rightRes + 1 else: count = count + leftRes + 1 return count for i in range(t): n = int(input()) arr = list(map(int, input().split(" "))) result = pump(arr, n) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
import sys def getMaxReservoir(sortedHills, pos, left, right): heigth, index = sortedHills[pos] while index < left or index >= right: pos += 1 heigth, index = sortedHills[pos] diff = min(right - index, index - left + 1) if diff < 3: return diff return 1 + min( getMaxReservoir(sortedHills, pos + 1, index + 1, right), getMaxReservoir(sortedHills, pos + 1, left, index), ) t = int(sys.stdin.readline()) while t > 0: n = int(sys.stdin.readline()) hs = list(map(int, sys.stdin.readline().split())) hsSorted = [] for i in range(0, n): hsSorted.append((hs[i], i)) hsSorted.sort(key=lambda tup: tup[0], reverse=True) print(getMaxReservoir(hsSorted, 0, 0, n)) t -= 1
IMPORT FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
import sys sys.setrecursionlimit(10000) def func(arr, c=0): if arr: c += 1 m = 0 for i in range(len(arr)): if arr[i] > m: j = i m = arr[i] if j == 0 or j == len(arr) - 1: return c else: a = func(arr[:j], c) b = func(arr[j + 1 :], c) return min(a, b) else: return c for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) c = func(arr) print(c)
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def main(): t = int(input()) for z in range(t): n = int(input()) li = list(map(int, input().split())) print(no_cal(li)) def no_cal(li): index = li.index(max(li)) if index == 0 or index == len(li) - 1: return 1 else: return 1 + min(no_cal(li[:index]), no_cal(li[index + 1 :])) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
for _ in range(0, int(input())): N = int(input()) count = 0 hillsH = list(map(int, input().split())) def FindAns(hills): maxIndex = 0 maxNum = max(hills) maxIndex = hills.index(maxNum) listRight = [] listLeft = [] if maxIndex == 0 or maxIndex == len(hills) - 1: return 1 for i in range(0, maxIndex): listLeft.append(hills[i]) for i in range(maxIndex + 1, len(hills)): listRight.append(hills[i]) return 1 + min(FindAns(listLeft), FindAns(listRight)) print(FindAns(hillsH))
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def minres(h, n): i = h.index(max(h)) return ( 1 if i == 0 or i == n - 1 else min(minres(h[:i], i), minres(h[i + 1 :], n - i - 1)) + 1 ) for _ in range(int(input())): n = int(input()) h = list(map(int, input().split())) print(minres(h, n))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
import sys def countr(s): highest = max(s) indexh = s.index(highest) n = len(s) if indexh == 0 or indexh == n - 1: return 1 left = s[:indexh] right = s[indexh + 1 :] return min(countr(left), countr(right)) + 1 def main(): T = int(input()) D = {} for i in range(T): N = int(input()) H = [int(item) for item in input().split()] D[i] = N, H for k, v in D.items(): N, H = v print(countr(H)) return 0 main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
T = int(input()) def Number(A): if len(A) == 0: return 0 elif len(A) == 1: return 1 x = max(A) if A.index(x) == 0 or A.index(x) == len(A) - 1: return 1 return 1 + min(Number(A[: A.index(x)]), Number(A[A.index(x) + 1 :])) for i in range(T): N = int(input()) H = list(map(int, input().split())) print(Number(H))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def solve(ar): max = 0 posn = -1 for i in range(len(ar)): if ar[i] > max: max = ar[i] posn = i if posn == 0 or posn == len(ar) - 1: return 1 return 1 + min(solve(ar[:posn]), solve(ar[posn + 1 :])) t = int(input()) for i in range(t): n = int(input()) h = list(map(int, input().split())) z = solve(h) print(z)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
t = int(input()) for i in range(t): n = int(input()) h = list(map(int, input().split())) def res(a): n = len(a) max1 = max(a) ind = 0 ind = a.index(max1) if ind == 0 or ind == n - 1: return 1 else: return 1 + min(res(a[:ind]), res(a[ind + 1 :])) print(res(h))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def rec(h, low, high): maxIndex = low for i in range(low + 1, high + 1): if h[i] > h[maxIndex]: maxIndex = i if maxIndex == low or maxIndex == high: return 1 else: return 1 + min(rec(h, low, maxIndex - 1), rec(h, maxIndex + 1, high)) t = int(input()) for T in range(t): n = int(input()) h = list(map(int, input().split())) print(rec(h, 0, n - 1))
FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def solve(st, ed): if st > ed: return 0 if st == ed: return 1 ab = max(b[st : ed + 1]) if ab[1] == st or ab[1] == ed: return 1 return 1 + min(solve(st, ab[1] - 1), solve(ab[1] + 1, ed)) for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] b = [[a[i], i] for i in range(n)] print(solve(0, n - 1))
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def res(arr): max_a = max(arr) if len(arr) == 0: return 0 if arr[0] == max_a or arr[-1] == max_a: return 1 else: index = arr.index(max_a) count_1 = res(arr[:index]) count_2 = res(arr[index + 1 :]) return min(count_2, count_1) + 1 t = int(input()) for a in range(t): n = int(input()) arr = [int(i) for i in input().split()] count = 0 max_a = max(arr) mid = len(arr) // 2 count = res(arr) print(count)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
t = int(input()) def maxcof(arr): i = 0 max = 0 for i in range(1, len(arr)): if arr[i] > arr[max]: max = i return max def splitarr(arr): x = maxcof(arr) temp1 = arr[x + 1 :] temp2 = arr[:x] if len(temp1) == 0 or len(temp2) == 0: return 1 else: m = min(splitarr(temp1), splitarr(temp2)) return m + 1 for i in range(t): n = int(input()) count = 0 inp = list(map(int, input().split())) print(splitarr(inp))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def divide(A, l, r): m = -1 maxi = 0 for i in range(l, r + 1): if A[i] > maxi: maxi = A[i] m = i if m == l or m == r: return 1 else: return 1 + min(divide(A, l, m - 1), divide(A, m + 1, r)) t = int(input()) while t > 0: n = int(input()) A = [int(x) for x in input().split()] k = divide(A, 0, n - 1) print(k) t -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def helper(l, count): le = len(l) if not l: return count count += 1 m = l.index(max(l)) if le - 1 == m or m == 0: return count x = helper(l[:m], count) y = helper(l[m + 1 :], count) if x < y: return x else: return y def func(): n = int(input()) l = list(map(int, input().strip().split())) print(helper(l, 0)) t = int(input()) for i in range(t): func()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
T = int(input()) def maxk(arr, start, end): maxno = arr[start] maxi = start for i in range(start, end): if arr[i] > maxno: maxno = arr[i] maxi = i return maxi def rec(arr, start, end): i = maxk(arr, start, end) if i == start or i == end - 1: return 1 else: return 1 + min(rec(arr, i + 1, end), rec(arr, start, i)) for t in range(T): n = int(input()) arr = list(map(int, input().split())) start = 0 end = n count = rec(arr, start, end) print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def f(height): ind = height.index(max(height)) n = len(height) if ind == 0 or ind == n - 1: return 1 else: return 1 + min(f(height[0:ind]), f(height[ind + 1 :])) test = int(input()) while test: n = int(input()) h = list(map(int, input().split())) print(f(h)) test = test - 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
t = int(input()) for _ in range(t): n = int(input()) lis = list(map(int, input().split())) def f(a, b): m = a for i in range(a, b + 1): if lis[m] < lis[i]: m = i if m == a or m == b: return 1 return 1 + min(f(a, m - 1), f(m + 1, b)) print(f(0, n - 1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
for i in range(int(input())): n = int(input()) h = list(map(int, input().split())) arr = [] arr.append(h) l = 0 bre = 0 while True: l = l + 1 c = [] for m in arr: x = m.index(max(m)) p = len(m) if x == 0 or x == p - 1: bre += 1 break else: c.append(m[:x]) c.append(m[x + 1 :]) arr = c[:] if bre > 0: break print(l)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def reservoir(a): ind = a.index(max(a)) if ind == len(a) - 1 or ind == 0: return 1 else: return min(reservoir(a[:ind]), reservoir(a[ind + 1 :])) + 1 for t in range(int(input())): n = int(input()) a = [] for i in input().split(): a.append(int(i)) print(reservoir(a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def find(l, h, a): m = -1 idx = 0 for i in range(l, h + 1): if m < a[i]: m = a[i] idx = i return idx def solve(l, h, a): m = find(l, h, a) if l == h: return 1 if m == l or m == h: return 1 else: return 1 + min(solve(l, m - 1, a), solve(m + 1, h, a)) for t in range(int(input())): n = int(input()) l = list(map(int, input().split())) print(solve(0, n - 1, l))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def coun(c, a): n = len(a) m = a.index(max(a)) if n == 1: return c + 1 elif m == 0 or m == n - 1: return c + 1 c1 = coun(c + 1, a[m + 1 :]) c2 = coun(c + 1, a[:m]) return min(c1, c2) t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) c = 0 print(coun(c, a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def get_max_index(heights, s, e): max_val = heights[s] index = s for i in range(s + 1, e + 1): if heights[i] > max_val: max_val = heights[i] index = i return index def get_min_num_of_reserviors(heights, i, j): if i > j: return 0 elif i == j: return 1 else: k = get_max_index(heights, i, j) if k == i or k == j: return 1 else: return 1 + min( get_min_num_of_reserviors(heights, i, k - 1), get_min_num_of_reserviors(heights, k + 1, j), ) T = int(input()) while T: N = int(input()) heights = list(map(int, input().split())) print(get_min_num_of_reserviors(heights, 0, N - 1)) T -= 1
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
t = int(input()) def largest(a, d, n): max = d for i in range(d, n): if a[i] > a[max]: max = i return max def pump(a, d, n): if d == n: return 0 cur = largest(a, d, n) if cur == d or cur == n - 1: return 1 else: e = pump(a, d, cur) f = pump(a, cur + 1, n) return 1 + min(e, f) while t: n = int(input()) a = list(map(int, input().split())) print(pump(a, 0, n)) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def func(l): ind = l.index(max(l)) n = len(l) if ind == 0 or ind == n - 1: return 1 else: return 1 + min(func(l[ind + 1 :]), func(l[:ind])) for i in range(int(input())): n = int(input()) l = [int(x) for x in input().split()] print(func(l))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
from sys import stdin max_val = int(10000000000000.0) min_val = int(-10000000000000.0) def read_int(): return int(stdin.readline()) def read_ints(): return [int(x) for x in stdin.readline().split()] def read_str(): return input() def read_strs(): return [x for x in stdin.readline().split()] def needed(hts): maxi = hts.index(max(hts)) if maxi == 0 or maxi == len(hts) - 1: return 1 else: return 1 + min(needed(hts[:maxi]), needed(hts[maxi + 1 :])) nb_test = read_int() for _ in range(nb_test): nb_hills = read_int() heights = read_ints() print(needed(heights))
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def find_max(A, s, e): m = 0 m_index = s for i in range(s, e + 1): if A[i] > m: m = A[i] m_index = i return m_index def solution(A, s, e, ans): m_index = find_max(A, s, e) if m_index == s or m_index == e: ans = ans + 1 return ans return min( solution(A, m_index + 1, e, ans + 1), solution(A, s, m_index - 1, ans + 1) ) T = int(input()) for _ in range(T): N = int(input()) A = [int(x) for x in input().split()] ans = solution(A, 0, N - 1, 0) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def short_path(h, lower, upper, maxi, dic): if lower == maxi or upper == maxi: return 1 else: return 1 + min( short_path(h, maxi + 1, upper, dic[max(h[maxi + 1 : upper + 1])], dic), short_path(h, lower, maxi - 1, dic[max(h[lower:maxi])], dic), ) t = int(input()) for it in range(t): size = int(input()) h = list(map(int, input().split())) dic = dict() for i in range(len(h)): dic[h[i]] = i print(short_path(h, 0, len(h) - 1, dic[max(h)], dic))
FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def solve(a, l, r): pos = None for i in range(l, r + 1): if pos == None or a[i] >= a[pos]: pos = i if pos == l or pos == r: return 1 return 1 + min(solve(a, l, pos - 1), solve(a, pos + 1, r)) def main(): t = int(input()) while t: n = int(input()) str = input() a = [int(x) for x in str.split()] print(solve(a, 0, n - 1)) t -= 1 main()
FUNC_DEF ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
In ChefLand, there is a mountain range consisting of $N$ hills (numbered $1$ through $N$) in a straight line. Let's denote the height of the $i$-th hill from the left by $h_i$. Ada is working on the water supply system of ChefLand. On some of the hills, she wants to place water reservoirs; then, for each reservoir, she will decide in which direction the water should flow from it β€” either to the left or to the right (water may not flow in both directions from the same reservoir). From a reservoir on a hill with height $h$, water flows in the chosen direction until it reaches the first hill that is strictly higher than $h$; all hills before this hill (including the hill containing the reservoir) are therefore supplied with water. For example, suppose we have hills with heights $[7, 2, 3, 5, 8]$. If we place a reservoir on the hill with height $5$, and pump water from it to the left, then the hills with heights $2$, $3$ and $5$ are supplied with water. Help Ada find the minimum numer of reservoirs needed to provide water to all the hills if she chooses the directions optimally. -----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 a single integer $N$. - The second line contains $N$ space-separated integers $h_1, h_2, \dots, h_N$. -----Output----- For each test case, print a single line containing one integer β€” the minimum required number of reservoirs. -----Constraints----- - $2 \le N \le 10^5$ - $1 \le h_i \le 10^9$ for each valid $i$ - $h_i \neq h_j $ for any valid $i \neq j$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 1 6 4 16 32 6 8 2 -----Example Output----- 2 -----Explanation----- Example case 1: We can place reservoirs on the second and third hill, pumping water to the left and right respectively.
def large(a, i, j): b = i maxo = a[i] t = i while b <= j: if a[b] > maxo: maxo = a[b] t = b b = b + 1 return t def f(a, i, j, count): p = large(a, i, j) if p == i or p == j: count = count + 1 return count else: return min(f(a, i, p - 1, count + 1), f(a, p + 1, j, count + 1)) t = int(input()) for x in range(t): n = int(input()) a = list(map(int, input().split())) print(f(a, 0, n - 1, 0))
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): l = [] def fun(x): if x: fun(x.left) l.append(x.data) fun(x.right) fun(root1) fun(root2) l.sort() return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): res = [] stk1 = [] stk2 = [] cur1 = root1 while cur1: stk1.append(cur1) cur1 = cur1.left cur2 = root2 while cur2: stk2.append(cur2) cur2 = cur2.left while stk1 and stk2: if stk1[-1].data < stk2[-1].data: cur = stk1.pop() res.append(cur.data) cur = cur.right while cur: stk1.append(cur) cur = cur.left else: cur = stk2.pop() res.append(cur.data) cur = cur.right while cur: stk2.append(cur) cur = cur.left while stk1: cur = stk1.pop() res.append(cur.data) cur = cur.right while cur: stk1.append(cur) cur = cur.left while stk2: cur = stk2.pop() res.append(cur.data) cur = cur.right while cur: stk2.append(cur) cur = cur.left return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): def build_tree_stack(root): stack = [] this_node = root while True: stack.append(this_node) if not this_node.left: return stack this_node = this_node.left def add_elem_to_list_admin(this_node, stack, sorted_elements_list): sorted_elements_list.append(this_node.data) stack.pop(-1) if this_node.right: stack += build_tree_stack(this_node.right) return stack, sorted_elements_list root1_stack = build_tree_stack(root1) root2_stack = build_tree_stack(root2) sorted_elements = [] while root1_stack and root2_stack: root1_elem = root1_stack[-1] root2_elem = root2_stack[-1] if root1_elem.data < root2_elem.data: root1_stack, sorted_elements = add_elem_to_list_admin( root1_elem, root1_stack, sorted_elements ) if root2_elem.data <= root1_elem.data: root2_stack, sorted_elements = add_elem_to_list_admin( root2_elem, root2_stack, sorted_elements ) leftover_stack = max([root1_stack, root2_stack]) while leftover_stack: this_node = leftover_stack[-1] leftover_stack, sorted_elements = add_elem_to_list_admin( this_node, leftover_stack, sorted_elements ) return sorted_elements
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): def dfs(ls, root): if not root: return None dfs(ls, root.left) ls.append(root.data) dfs(ls, root.right) return ls ls1 = dfs([], root1) ls2 = dfs([], root2) res = [] i1 = 0 i2 = 0 while i1 < len(ls1) and i2 < len(ls2): if ls1[i1] < ls2[i2]: res.append(ls1[i1]) i1 += 1 else: res.append(ls2[i2]) i2 += 1 if i1 < len(ls1): res = res + ls1[i1:] if i2 < len(ls2): res = res + ls2[i2:] return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def Isorder1(self, root1, l1): if root1 is None: return self.Isorder1(root1.left, l1) l1.append(root1.data) self.Isorder1(root1.right, l1) def Isorder2(self, root2, l2): if root2 is None: return self.Isorder2(root2.left, l2) l2.append(root2.data) self.Isorder2(root2.right, l2) def merge(self, root1, root2): l1 = [] l2 = [] self.Isorder1(root1, l1) self.Isorder2(root2, l2) l3 = l1 + l2 l3.sort() return l3
CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def traversal(self, root): if root.left: yield from self.traversal(root.left) yield root.data if root.right: yield from self.traversal(root.right) def nxt(self, i): try: a = next(i) except StopIteration as e: return None return a def merge(self, root1, root2): i1 = self.traversal(root1) i2 = self.traversal(root2) r = [] a = self.nxt(i1) b = self.nxt(i2) while True: if not a and not b: break elif a and b: if a <= b: while a and a <= b: r.append(a) a = self.nxt(i1) else: while b and b < a: r.append(b) b = self.nxt(i2) elif a and not b: while a: r.append(a) a = self.nxt(i1) elif b and not a: while b: r.append(b) b = self.nxt(i2) return r
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR VAR IF VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN NONE RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR IF VAR VAR IF VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorderTraversal(self, root): ll = [] if root.left: ll += self.inorderTraversal(root.left) ll.append(root.data) if root.right: ll += self.inorderTraversal(root.right) return ll def merge(self, root1, root2): ll1, ll2 = [], [] ll1 += self.inorderTraversal(root1) ll2 += self.inorderTraversal(root2) for i in ll2: ll1.append(i) ll1.sort() return ll1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR LIST LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): ans1 = [] ans2 = [] self.inorder(root1, ans1) self.inorder(root2, ans2) return sorted(ans1 + ans2) def inorder(self, root, res): if root is None: return res self.inorder(root.left, res) res.append(root.data) self.inorder(root.right, res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder(self, root, inord): if root: self.inorder(root.left, inord) inord.append(root.data) self.inorder(root.right, inord) def merge_arrays(self, arr1, arr2): n1 = len(arr1) n2 = len(arr2) if n1 == 0: return arr2 if n2 == 0: return arr1 arr3 = [-1] * (n1 + n2) i = j = k = 0 while i < n1 and j < n2: if arr1[i] <= arr2[j]: arr3[k] = arr1[i] i += 1 else: arr3[k] = arr2[j] j += 1 k += 1 while i < n1: arr3[k] = arr1[i] i += 1 k += 1 while j < n2: arr3[k] = arr2[j] j += 1 k += 1 return arr3 def create_bst(self, arr, start, end): if start > end: return mid = start + (end - start) // 2 root = Node(arr[mid]) root.left = self.create_bst(arr, start, mid - 1) root.right = self.create_bst(arr, mid + 1, end) return root def merge(self, root1, root2): inord_1 = [] self.inorder(root1, inord_1) inord_2 = [] self.inorder(root2, inord_2) arr = self.merge_arrays(inord_1, inord_2) return arr
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): p1, p2 = [], [] def inorder(root, pos): if not root: return None inorder(root.left, pos) pos.append(root.data) inorder(root.right, pos) inorder(root1, p1) inorder(root2, p2) res = p1 + p2 res.sort() return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST FUNC_DEF IF VAR RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
def Inorder(root, arr): if root == None: return Inorder(root.left, arr) arr.append(root.data) Inorder(root.right, arr) def Merge2SortedArray(arr1, arr2): indx1 = 0 indx2 = 0 size1 = len(arr1) size2 = len(arr2) ans = [] while indx1 < size1 and indx2 < size2: if arr1[indx1] < arr2[indx2]: ans.append(arr1[indx1]) indx1 += 1 else: ans.append(arr2[indx2]) indx2 += 1 while indx1 < size1: ans.append(arr1[indx1]) indx1 += 1 while indx2 < size2: ans.append(arr2[indx2]) indx2 += 1 return ans def ArraytoBST(arr, start, end): if start > end: return None n = (start + end) // 2 root = Node(arr[n]) root.left = ArraytoBST(arr, start, n - 1) root.right = ArraytoBST(arr, n + 1, end) return root class Solution: def merge(self, root1, root2): if root1 == None and root2 == None: return [] arr1 = [] Inorder(root1, arr1) arr2 = [] Inorder(root2, arr2) if len(arr1) == 0: return arr2 if len(arr2) == 0: return arr1 ans = Merge2SortedArray(arr1, arr2) return ans
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): inorder1 = self.InorderIterative(root1) inorder2 = self.InorderIterative(root2) merged_array = self.mergeSortedArray( inorder1, len(inorder1), inorder2, len(inorder2) ) return merged_array root = self.sortedArrayToBST(merged_array) return self.InorderIterative(root) def InorderIterative(self, root): if root == None: return stack, ans = [], [] while stack or root: while root: stack.append(root) root = root.left curr = stack.pop() ans.append(curr.data) root = curr.right return ans def mergeSortedArray(self, nums1, m, nums2, n): i, j = 0, 0 ans = [] while i < m and j < n: if nums1[i] >= nums2[j]: ans.append(nums2[j]) j += 1 else: ans.append(nums1[i]) i += 1 while i < m: ans.append(nums1[i]) i += 1 while j < n: ans.append(nums2[j]) j += 1 return ans def sortedArrayToBST(self, nums): if not nums: return None mid = len(nums) // 2 node = Node(nums[mid]) node.left = self.sortedArrayToBST(nums[:mid]) node.right = self.sortedArrayToBST(nums[mid + 1 :]) return node
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR LIST LIST WHILE VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
def insert(root, l): if root: insert(root.left, l) l.append(root.data) insert(root.right, l) return l class Solution: def merge(self, root1, root2): l1 = [] l2 = [] insert(root1, l1) insert(root2, l2) return sorted(l1 + l2)
FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): l1, l2 = [], [] def inorder(root, a): if root: inorder(root.left, a) a.append(root.data) inorder(root.right, a) inorder(root1, l1) inorder(root2, l2) k = [] i, j = 0, 0 m, n = len(l1), len(l2) while i < m and j < n: if l1[i] < l2[j]: k.append(l1[i]) i += 1 else: k.append(l2[j]) j += 1 if i < m: k.extend(l1[i:]) else: k.extend(l2[j:]) return k
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder(self, root, arr): if root is not None: self.inorder(root.left, arr) arr.append(root.data) self.inorder(root.right, arr) def merge(self, root1, root2): arr = [] self.inorder(root1, arr) self.inorder(root2, arr) arr.sort() return arr
CLASS_DEF FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder(self, root, inOrder): if root is None: return None self.inorder(root.left, inOrder) inOrder.append(root.data) self.inorder(root.right, inOrder) def mergeTwoSortedArrays(self, arr1, arr2): i, j, res = 0, 0, [] while i < len(arr1) and j < len(arr2): if arr1[i] < arr2[j]: res.append(arr1[i]) i += 1 else: res.append(arr2[j]) j += 1 while i < len(arr1): res.append(arr1[i]) i += 1 while j < len(arr2): res.append(arr2[j]) j += 1 return res def inorderToBST(self, s, e, inOrder): if s > e: return None mid = (s + e) // 2 root = Node(inOrder[mid]) root.left = self.inorderToBST(s, mid - 1, inOrder) root.right = self.inorderToBST(mid + 1, e, inOrder) return root def merge(self, root1, root2): bst1, bst2 = [], [] self.inorder(root1, bst1) self.inorder(root2, bst2) mergeArray = self.mergeTwoSortedArrays(bst1, bst2) return mergeArray
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, r1, r2): sol = [] stk1, stk2 = [], [] f1, f2, s1, s2 = 1, 1, 1, 1 while f1 or f2: while f1 and s1: if r1: stk1.append(r1) r1 = r1.left elif stk1: r1 = stk1.pop() d1 = r1.data r1 = r1.right break else: f1 = 0 while f2 and s2: if r2: stk2.append(r2) r2 = r2.left elif stk2: r2 = stk2.pop() d2 = r2.data r2 = r2.right break else: f2 = 0 if f1 and f2: if d1 < d2: sol.append(d1) s1 = 1 s2 = 0 else: sol.append(d2) s1 = 0 s2 = 1 elif f1: sol.append(d1) s1 = 1 elif f2: sol.append(d2) s2 = 1 return sol
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): stack1, stack2 = [], [] res = [] cur1, cur2 = root1, root2 while cur1: stack1.append(cur1) cur1 = cur1.left while cur2: stack2.append(cur2) cur2 = cur2.left while stack1 or stack2: item1, item2 = None, None if stack1: item1 = stack1[-1] if stack2: item2 = stack2[-1] if item1 and item2: if item1.data <= item2.data: res.append(item1.data) stack1.pop() item2 = None else: res.append(item2.data) stack2.pop() item1 = None elif item1: res.append(item1.data) stack1.pop() else: res.append(item2.data) stack2.pop() if item1 and item1.right: item1 = item1.right while item1: stack1.append(item1) item1 = item1.left if item2 and item2.right: item2 = item2.right while item2: stack2.append(item2) item2 = item2.left return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR NONE NONE IF VAR ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): def inorder(root, ino): if not root: return inorder(root.left, ino) ino.append(root.data) inorder(root.right, ino) ino1, ino2 = [], [] inorder(root1, ino1) inorder(root2, ino2) return sorted(ino1 + ino2)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): list1 = [] list2 = [] def inorder(node, lst): if not node: return inorder(node.left, lst) lst.append(node.data) inorder(node.right, lst) inorder(root1, list1) inorder(root2, list2) i = j = 0 merged_list = [] while i < len(list1) and j < len(list2): if list1[i] < list2[j]: merged_list.append(list1[i]) i += 1 else: merged_list.append(list2[j]) j += 1 merged_list += list1[i:] merged_list += list2[j:] return merged_list
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): ans = [] s1, s2 = [], [] while root1 or root2 or s1 or s2: while root1: s1.append(root1) root1 = root1.left while root2: s2.append(root2) root2 = root2.left if not s2 or s1 and s1[-1].data <= s2[-1].data: root1 = s1[-1] del s1[-1] ans.append(root1.data) root1 = root1.right else: root2 = s2[-1] del s2[-1] ans.append(root2.data) root2 = root2.right return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST WHILE VAR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def getList(self, root, ar): if not root: return self.getList(root.left, ar) ar.append(root.data) self.getList(root.right, ar) def merge(self, root1, root2): ar1 = [] ar2 = [] self.getList(root1, ar1) self.getList(root2, ar2) res = [] i = j = 0 while i < len(ar1) and j < len(ar2): if ar1[i] < ar2[j]: res.append(ar1[i]) i += 1 else: res.append(ar2[j]) j += 1 while i < len(ar1): res.append(ar1[i]) i += 1 while j < len(ar2): res.append(ar2[j]) j += 1 return res
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2) -> list: result = [] if root1: self._merge(root1, result) if root2: self._merge(root2, result) result.sort() return result def _merge(self, node, result: list) -> None: if node is None: return result.append(node.data) self._merge(node.left, result) self._merge(node.right, result)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF VAR IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NONE
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): x = self.inorder(root1, []) + self.inorder(root2, []) x.sort() return x def inorder(self, node, a): if node is None: return else: self.inorder(node.left, a) a.append(node.data) self.inorder(node.right, a) return a
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): lst1, lst2 = [], [] result = [] def dfs(node, lst): if node is None: return lst.append(node) dfs(node.left, lst) dfs(root1, lst1) dfs(root2, lst2) while lst1 or lst2: if lst1 and lst2: if lst1[-1].data <= lst2[-1].data: node = lst1.pop() result.append(node.data) dfs(node.right, lst1) else: node = lst2.pop() result.append(node.data) dfs(node.right, lst2) elif lst1: node = lst1.pop() result.append(node.data) dfs(node.right, lst1) elif lst2: node = lst2.pop() result.append(node.data) dfs(node.right, lst2) return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder(self, root): arr = [] if not root: return arr arr += self.inorder(root.left) arr.append(root.data) arr += self.inorder(root.right) return arr def merge(self, root1, root2): arr1 = self.inorder(root1) arr2 = self.inorder(root2) i = j = 0 sortedArr = [] while i < len(arr1) and j < len(arr2): if arr1[i] < arr2[j]: sortedArr.append(arr1[i]) i += 1 else: sortedArr.append(arr2[j]) j += 1 if i < len(arr1): sortedArr += arr1[i:] if j < len(arr2): sortedArr += arr2[j:] return sortedArr
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR RETURN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: arr = [] brr = [] def gem(self, root, brr): if root == None: return self.gen(root.left, brr) brr.append(root.data) self.gen(root.right, brr) def gen(self, root, arr): if root == None: return self.gen(root.left, arr) arr.append(root.data) self.gen(root.right, arr) def merge(self, root1, root2): arr = [] brr = [] self.gen(root1, arr) self.gem(root2, brr) i = 0 j = 0 ans = [] while i < len(arr) and j < len(brr): if arr[i] < brr[j]: ans.append(arr[i]) i += 1 else: ans.append(brr[j]) j += 1 while i < len(arr): ans.append(arr[i]) i += 1 while j < len(brr): ans.append(brr[j]) j += 1 return ans return []
CLASS_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR RETURN LIST
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): stack1 = [] node1 = root1 stack2 = [] node2 = root2 result = [] while node1 or stack1 or node2 or stack2: while node1: stack1.append(node1) node1 = node1.left while node2: stack2.append(node2) node2 = node2.left if not stack2 or stack1 and stack1[-1].data < stack2[-1].data: node1 = stack1.pop() result.append(node1.data) node1 = node1.right else: node2 = stack2.pop() result.append(node2.data) node2 = node2.right return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): stack1 = [] stack2 = [] result = [] while True: while root1 or root2: if root1: stack1.append(root1) root1 = root1.left if root2: stack2.append(root2) root2 = root2.left if stack1 or stack2: if not stack1: root2 = stack2.pop() result.append(root2.data) root2 = root2.right elif not stack2: root1 = stack1.pop() result.append(root1.data) root1 = root1.right elif stack1[-1].data < stack2[-1].data: root1 = stack1.pop() result.append(root1.data) root1 = root1.right else: root2 = stack2.pop() result.append(root2.data) root2 = root2.right else: break return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST WHILE NUMBER WHILE VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): def helper(root): x = [] if not root: return x x += helper(root.left) x.append(root.data) x += helper(root.right) return x return sorted(helper(root1) + helper(root2))
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST IF VAR RETURN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): s1 = [] s2 = [] res = [] cur1 = root1 cur2 = root2 while True: while cur1: s1.append(cur1) cur1 = cur1.left while cur2: s2.append(cur2) cur2 = cur2.left if not s1 and not s2: break elif not s1: res.append(s2[-1].data) cur2 = s2.pop().right elif not s2: res.append(s1[-1].data) cur1 = s1.pop().right elif s1[-1].data < s2[-1].data: res.append(s1[-1].data) cur1 = s1.pop().right else: res.append(s2[-1].data) cur2 = s2.pop().right return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): inorder1 = self.inorder(root1) inorder2 = self.inorder(root2) return sorted(inorder1 + inorder2) def inorder(self, root): if not root: return [] return self.inorder(root.left) + [root.data] + self.inorder(root.right)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF IF VAR RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): def bele(root, a): if root: a.append(root.data) else: return if root.left: bele(root.left, a) if root.right: bele(root.right, a) p = [] q = [] bele(root1, p) bele(root2, q) p += q p.sort() return p
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): def inorder(root): nonlocal arr if root: inorder(root.left) arr.append(root.data) inorder(root.right) arr = [] inorder(root1) p = len(arr) inorder(root2) res = [] i, j = 0, p while i < p and j < len(arr): if arr[i] < arr[j]: res.append(arr[i]) i += 1 else: res.append(arr[j]) j += 1 while i < p: res.append(arr[i]) i += 1 while j < len(arr): res.append(arr[j]) j += 1 return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): lis1 = [] self.sorts(root1, lis1) lis2 = [] self.sorts(root2, lis2) lis2 = lis1 + lis2 lis2.sort() return lis2 def sorts(self, root, lis): if root == None: return self.sorts(root.left, lis) lis.append(root.data) self.sorts(root.right, lis) return lis
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder1(self, root1): if root1: self.inorder1(root1.left) self.res.append(root1.data) self.inorder1(root1.right) def inorder2(self, root2): if root2: self.inorder1(root2.left) self.res.append(root2.data) self.inorder1(root2.right) def merge(self, root1, root2): self.res = [] self.inorder1(root1) self.inorder2(root2) self.res.sort() return self.res
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder(self, root, inord): if root: self.inorder(root.left, inord) inord.append(root.data) self.inorder(root.right, inord) def convert_bst_to_dll(self, root, head): if root == None: return self.convert_bst_to_dll(root.right, head) root.right = head[0] head[0] = root self.convert_bst_to_dll(root.left, head) def merge_lists(self, head1, head2): head = tail = None while head1 and head2: if head1.data < head2.data: if head == None: head = tail = head1 else: tail.right = head1 tail = head1 head1 = head1.right else: if head == None: head = tail = head2 else: tail.right = head2 tail = head2 head2 = head2.right while head1: tail.right = head1 head1.left = tail tail = head1 head1 = head1.right while head2: tail.right = head2 head2.left = tail tail = head2 head2 = head2.right return head def count_nodes(self, head): count = 0 temp = head while temp: count += 1 temp = temp.right return count def dll_to_bst(self, head, n): if n <= 0 or head[0] == None: return left = self.dll_to_bst(head, n // 2) root = head[0] root.left = left head[0] = head[0].right root.right = self.dll_to_bst(head, n - n // 2 - 1) return root def merge(self, root1, root2): head1 = [None] self.convert_bst_to_dll(root1, head1) head2 = [None] self.convert_bst_to_dll(root2, head2) head = self.merge_lists(head1[0], head2[0]) head = [head] n = self.count_nodes(head[0]) root = self.dll_to_bst(head, n) inord = [] self.inorder(root, inord) return inord
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NONE WHILE VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): req = [] lst1, lst2 = [], [] while root1: lst1.append(root1) root1 = root1.left while root2: lst2.append(root2) root2 = root2.left x, y = get(lst1), get(lst2) while x and y: if x.data > y.data: req.append(y.data) y = get(lst2) else: req.append(x.data) x = get(lst1) while x: req.append(x.data) x = get(lst1) while y: req.append(y.data) y = get(lst2) return req def get(lst): if not lst: return None x = lst.pop() if x.right: temp = x.right while temp: lst.append(temp) temp = temp.left return x
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): in1 = [] in2 = [] self.inorder(root1, in1) self.inorder(root2, in2) res = [] m = len(in1) n = len(in2) i = 0 j = 0 while i < m and j < n: if in1[i] < in2[j]: res.append(in1[i]) i += 1 else: res.append(in2[j]) j += 1 while i < m: res.append(in1[i]) i += 1 while j < n: res.append(in2[j]) j += 1 return res def inorder(self, root, res): if not root: return self.inorder(root.left, res) res.append(root.data) self.inorder(root.right, res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): res = [] array1 = self.inorder(root1, []) array2 = self.inorder(root2, []) i, j = 0, 0 while i < len(array1) and j < len(array2): if array1[i] < array2[j]: res.append(array1[i]) i += 1 else: res.append(array2[j]) j += 1 while i < len(array1): res.append(array1[i]) i += 1 while j < len(array2): res.append(array2[j]) j += 1 return res def inorder(self, root, arr): if root is not None: self.inorder(root.left, arr) arr.append(root.data) self.inorder(root.right, arr) return arr
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder(self, p, li): if p: self.inorder(p.left, li) li.append(p.data) self.inorder(p.right, li) def mergify(self, t1=None, t2=None): li = [] li1 = [] if t1 and t2: self.inorder(t1, li) self.inorder(t2, li1) elif t1: self.inorder(t1, li) elif t2: self.inorder(t2, li1) i = j = k = 0 result = [None] * (len(li) + len(li1)) while i < len(li) and j < len(li1): if li[i] <= li1[j]: result[k] = li[i] i += 1 else: result[k] = li1[j] j += 1 k += 1 while i < len(li): result[k] = li[i] i += 1 k += 1 while j < len(li1): result[k] = li1[j] k += 1 j += 1 return result def merge(self, t1, t2): if t1 is None and t2 is None: return elif t1 is None: return self.mergify(t2) elif t2 is None: return self.mergify(t1) else: return self.mergify(t1, t2)
CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF NONE NONE ASSIGN VAR LIST ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN IF VAR NONE RETURN FUNC_CALL VAR VAR IF VAR NONE RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): def inorder(root, li): if not root: return inorder(root.left, li) li.append(root.data) inorder(root.right, li) return def mergeTwoSortedArr(li1, li2, ans): i = 0 j = 0 while i < len(li1) and j < len(li2): if li1[i] <= li2[j]: ans.append(li1[i]) i += 1 else: ans.append(li2[j]) j += 1 if i == len(li1): ans.extend(li2[j:]) elif j == len(li2): ans.extend(li1[i:]) li1 = [] li2 = [] ans = [] inorder(root1, li1) inorder(root2, li2) mergeTwoSortedArr(li1, li2, ans) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): first, second = [], [] while root1: first.append(root1) root1 = root1.left while root2: second.append(root2) root2 = root2.left ans = [] while len(first) or len(second): if len(first) and (len(second) == 0 or first[-1].data <= second[-1].data): X = first.pop() ans += [X.data] if X.right: X = X.right while X: first.append(X) X = X.left else: X = second.pop() ans += [X.data] if X.right: X = X.right while X: second.append(X) X = X.left return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR IF VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR IF VAR ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
def recGenerator(node: Node): if node.left is not None: for value in recGenerator(node.left): yield value yield node.data if node.right is not None: for value in recGenerator(node.right): yield value class Solution: def merge(self, root1, root2): a = [i for i in recGenerator(root1)] b = [i for i in recGenerator(root2)] res = [] Aind, Bind = 0, 0 while Aind != len(a) and Bind != len(b): if a[Aind] > b[Bind]: res.append(b[Bind]) Bind += 1 else: res.append(a[Aind]) Aind += 1 while Aind != len(a): res.append(a[Aind]) Aind += 1 while Bind != len(b): res.append(b[Bind]) Bind += 1 return res
FUNC_DEF VAR IF VAR NONE FOR VAR FUNC_CALL VAR VAR EXPR VAR EXPR VAR IF VAR NONE FOR VAR FUNC_CALL VAR VAR EXPR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): l1 = [] l2 = [] st = [] while st or root1: while root1: st.append(root1) root1 = root1.left node = st.pop() l1.append(node.data) root1 = node.right st = [] while st or root2: while root2: st.append(root2) root2 = root2.left node = st.pop() l2.append(node.data) root2 = node.right l = [] i, j = 0, 0 while i < len(l1) and j < len(l2): if l1[i] <= l2[j]: l.append(l1[i]) i += 1 else: l.append(l2[j]) j += 1 while i < len(l1): l.append(l1[i]) i += 1 while j < len(l2): l.append(l2[j]) j += 1 return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def inorder(self, root, path): if not root: return self.inorder(root.left, path) path.append(root.data) self.inorder(root.right, path) def merge(self, root1, root2): path1, path2 = [], [] self.inorder(root1, path1) self.inorder(root2, path2) i, j, n, m = 0, 0, len(path1), len(path2) res = [] while i < n and j < m: if path1[i] <= path2[j]: res.append(path1[i]) i += 1 else: res.append(path2[j]) j += 1 while i < n: res.append(path1[i]) i += 1 while j < m: res.append(path2[j]) j += 1 return res
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): inorder1 = [] def findinorder(root): if root == None: return None findinorder(root.left) inorder1.append(root.data) findinorder(root.right) findinorder(root1) inorder2 = [] def findinorder2(root): if root == None: return None findinorder2(root.left) inorder2.append(root.data) findinorder2(root.right) findinorder2(root2) res = [] i = 0 j = 0 while i < len(inorder1) and j < len(inorder2): if inorder1[i] <= inorder2[j]: res.append(inorder1[i]) i += 1 else: res.append(inorder2[j]) j += 1 while i < len(inorder1): res.append(inorder1[i]) i += 1 while j < len(inorder2): res.append(inorder2[j]) j += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): ans1 = [] ans2 = [] def find(ans, root): if root is None: return None find(ans, root.left) ans.append(root.data) find(ans, root.right) find(ans1, root1) find(ans2, root2) return sorted(ans1 + ans2)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR
Given two BSTs, return elements of both BSTs in sorted form. Example 1: Input: BST1: 5 / \ 3 6 / \ 2 4 BST2: 2 / \ 1 3 \ 7 / 6 Output: 1 2 2 3 3 4 5 6 6 7 Explanation: After merging and sorting the two BST we get 1 2 2 3 3 4 5 6 6 7. Example 2: Input: BST1: 12 / 9 / \ 6 11 BST2: 8 / \ 5 10 / 2 Output: 2 5 6 8 9 10 11 12 Explanation: After merging and sorting the two BST we get 2 5 6 8 9 10 11 12. Your Task: You don't need to read input or print anything. Your task is to complete the function merge() which takes roots of both the BSTs as its input and returns an array of integers denoting the node values of both the BSTs in a sorted order. Expected Time Complexity: O(M+N) where M and N are the sizes of the two BSTs. Expected Auxiliary Space: O(Height of BST1 + Height of BST2 + M + N(for storing the answer)). Constraints: 1 ≀ Number of Nodes ≀ 10^{5}
class Solution: def merge(self, root1, root2): lst = [] ans1 = self.preOrder(root1, lst) lst = [] ans2 = self.preOrder(root2, lst) lst = [] lst = ans1 + ans2 return sorted(lst) def preOrder(self, root, lst): if root is None: return [] lst.append(root.data) self.preOrder(root.left, lst) self.preOrder(root.right, lst) return lst
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR 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: ans = -float("inf") def tra(self, root): if root: return max(self.tra(root.left), self.tra(root.right)) * root.data else: return 1 def findMaxScore(self, root): return self.tra(root)
CLASS_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER 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 findMaxScore(self, root): k = [] r = [] value = [] value1 = [] max1 = -10000 k.append(root) if root.data > max1: max1 = root.data value.append(max1) while k != []: l = k.pop(0) m = value.pop(0) if l.left: if l.left.data * m > max1: max1 = l.left.data * m r.append(l.left) value1.append(l.left.data * m) if l.right: if l.right.data * m > max1: max1 = l.right.data * m r.append(l.right) value1.append(l.right.data * m) if k == []: k = r value = value1 r = [] value1 = [] return max1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST 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): return self.step(root, 1) def step(self, node, val): if not node: return val sol1 = self.step(node.left, val * node.data) sol2 = self.step(node.right, val * node.data) return max(sol1, sol2)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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): if root == None: return 0 if root.left == None and root.right == None: return root.data return root.data * max( self.findMaxScore(root.left), self.findMaxScore(root.right) )
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN VAR 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 __init__(self): self.result = 1 def findMaxScore(self, root): def fxn(node, back_value): if node: if node.left is None and node.right is None: self.result = max(self.result, node.data * back_value) fxn(node.left, node.data * back_value) fxn(node.right, node.data * back_value) fxn(root, 1) return self.result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF FUNC_DEF IF VAR IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR